Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timers: assorted cleanup + improvements #18486

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ Timeout.prototype[refreshFnSymbol] = function refresh() {
// Would be more ideal with uv_timer_again(), however that API does not
// cause libuv's sorted timers data structure (a binary heap at the time
// of writing) to re-sort itself. This causes ordering inconsistencies.
this._handle.stop();
this._handle.start(this._idleTimeout);
} else if (this[unrefedSymbol]) {
getTimers()._unrefActive(this);
Expand Down
88 changes: 30 additions & 58 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ function TimersList(msecs, unrefed) {
this._idlePrev = this; // prevent any unnecessary hidden class changes.
this._unrefed = unrefed;
this.msecs = msecs;
this.nextTick = false;

const timer = this._timer = new TimerWrap();
timer._list = this;
Expand All @@ -222,19 +221,11 @@ function TimersList(msecs, unrefed) {

// adds listOnTimeout to the C++ object prototype, as
// V8 would not inline it otherwise.
TimerWrap.prototype[kOnTimeout] = function listOnTimeout() {
var list = this._list;
var msecs = list.msecs;

if (list.nextTick) {
list.nextTick = false;
process.nextTick(listOnTimeoutNT, list);
return;
}
TimerWrap.prototype[kOnTimeout] = function listOnTimeout(now) {
const list = this._list;
const msecs = list.msecs;

debug('timeout callback %d', msecs);

var now = TimerWrap.now();
debug('now: %d', now);

var diff, timer;
Expand All @@ -245,12 +236,12 @@ TimerWrap.prototype[kOnTimeout] = function listOnTimeout() {
// This happens if there are more timers scheduled for later in the list.
if (diff < msecs) {
var timeRemaining = msecs - (TimerWrap.now() - timer._idleStart);
if (timeRemaining < 0) {
if (timeRemaining <= 0) {
timeRemaining = 1;
}
this.start(timeRemaining);
debug('%d list wait because diff is %d', msecs, diff);
return;
return true;
}

// The actual logic for when a timeout happens.
Expand Down Expand Up @@ -287,10 +278,10 @@ TimerWrap.prototype[kOnTimeout] = function listOnTimeout() {

// Do not close the underlying handle if its ownership has changed
// (e.g it was unrefed in its callback).
if (this.owner)
return;
if (!this.owner)
this.close();

this.close();
return true;
};


Expand All @@ -316,34 +307,10 @@ function tryOnTimeout(timer, list) {
timer._destroyed = true;
}
}

if (!threw) return;

// Postpone all later list events to next tick. We need to do this
// so that the events are called in the order they were created.
const lists = list._unrefed === true ? unrefedLists : refedLists;
for (var key in lists) {
if (key > list.msecs) {
lists[key].nextTick = true;
}
}
// We need to continue processing after domain error handling
// is complete, but not by using whatever domain was left over
// when the timeout threw its exception.
const domain = process.domain;
process.domain = null;
// If we threw, we need to process the rest of the list in nextTick.
process.nextTick(listOnTimeoutNT, list);
process.domain = domain;
}
}


function listOnTimeoutNT(list) {
list._timer[kOnTimeout]();
}


// A convenience function for re-using TimerWrap handles more easily.
//
// This mostly exists to fix https://github.com/nodejs/node/issues/1264.
Expand All @@ -353,7 +320,7 @@ function listOnTimeoutNT(list) {
function reuse(item) {
L.remove(item);

var list = refedLists[item._idleTimeout];
const list = refedLists[item._idleTimeout];
// if empty - reuse the watcher
if (list !== undefined && L.isEmpty(list)) {
debug('reuse hit');
Expand All @@ -377,7 +344,7 @@ const unenroll = exports.unenroll = function(item) {
item._destroyed = true;
}

var handle = reuse(item);
const handle = reuse(item);
if (handle !== null) {
debug('unenroll: list empty');
handle.close();
Expand Down Expand Up @@ -449,11 +416,12 @@ setTimeout[internalUtil.promisify.custom] = function(after, value) {
exports.setTimeout = setTimeout;


function ontimeout(timer) {
var args = timer._timerArgs;
function ontimeout(timer, start) {
const args = timer._timerArgs;
if (typeof timer._onTimeout !== 'function')
return promiseResolve(timer._onTimeout, args[0]);
const start = TimerWrap.now();
if (start === undefined && timer._repeat)
start = TimerWrap.now();
if (!args)
timer._onTimeout();
else
Expand Down Expand Up @@ -537,26 +505,30 @@ exports.clearInterval = function(timer) {
};


function unrefdHandle() {
// Don't attempt to call the callback if it is not a function.
if (typeof this.owner._onTimeout === 'function') {
ontimeout(this.owner);
function unrefdHandle(now) {
try {
// Don't attempt to call the callback if it is not a function.
if (typeof this.owner._onTimeout === 'function') {
ontimeout(this.owner, now);
}
} finally {
// Make sure we clean up if the callback is no longer a function
// even if the timer is an interval.
if (!this.owner._repeat ||
typeof this.owner._onTimeout !== 'function') {
this.owner.close();
}
}

// Make sure we clean up if the callback is no longer a function
// even if the timer is an interval.
if (!this.owner._repeat ||
typeof this.owner._onTimeout !== 'function') {
this.owner.close();
}
return true;
}


Timeout.prototype.unref = function() {
if (this._handle) {
this._handle.unref();
} else if (typeof this._onTimeout === 'function') {
var now = TimerWrap.now();
const now = TimerWrap.now();
if (!this._idleStart) this._idleStart = now;
var delay = this._idleStart + this._idleTimeout - now;
if (delay < 0) delay = 0;
Expand All @@ -567,7 +539,7 @@ Timeout.prototype.unref = function() {
return;
}

var handle = reuse(this);
const handle = reuse(this);
if (handle !== null) {
handle._list = undefined;
}
Expand Down
8 changes: 8 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ inline bool Environment::TickInfo::has_scheduled() const {
return fields_[kHasScheduled] == 1;
}

inline bool Environment::TickInfo::has_thrown() const {
return fields_[kHasThrown] == 1;
}

inline bool Environment::TickInfo::has_promise_rejections() const {
return fields_[kHasPromiseRejections] == 1;
}
Expand All @@ -272,6 +276,10 @@ inline void Environment::TickInfo::promise_rejections_toggle_on() {
fields_[kHasPromiseRejections] = 1;
}

inline void Environment::TickInfo::set_has_thrown(bool state) {
fields_[kHasThrown] = state ? 1 : 0;
}

inline void Environment::AssignToContext(v8::Local<v8::Context> context,
const ContextInfo& info) {
context->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, this);
Expand Down
3 changes: 3 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,10 @@ class Environment {
inline AliasedBuffer<uint8_t, v8::Uint8Array>& fields();
inline bool has_scheduled() const;
inline bool has_promise_rejections() const;
inline bool has_thrown() const;

inline void promise_rejections_toggle_on();
inline void set_has_thrown(bool state);

private:
friend class Environment; // So we can call the constructor.
Expand All @@ -489,6 +491,7 @@ class Environment {
enum Fields {
kHasScheduled,
kHasPromiseRejections,
kHasThrown,
kFieldsCount
};

Expand Down
5 changes: 5 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,10 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
AsyncWrap::EmitBefore(env, asyncContext.async_id);
}

if (!IsInnerMakeCallback()) {
env->tick_info()->set_has_thrown(false);
}

env->async_hooks()->push_async_ids(async_context_.async_id,
async_context_.trigger_async_id);
pushed_ids_ = true;
Expand Down Expand Up @@ -984,6 +988,7 @@ void InternalCallbackScope::Close() {
Local<Object> process = env_->process_object();

if (env_->tick_callback_function()->Call(process, 0, nullptr).IsEmpty()) {
env_->tick_info()->set_has_thrown(true);
failed_ = true;
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
Expand Down Expand Up @@ -138,19 +139,32 @@ class TimerWrap : public HandleWrap {
Environment* env = wrap->env();
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
wrap->MakeCallback(kOnTimeout, 0, nullptr);
Local<Value> ret;
Local<Value> args[1];
do {
args[0] = GetNow(env);
ret = wrap->MakeCallback(kOnTimeout, 1, args).ToLocalChecked();
} while (ret->IsUndefined() &&
!env->tick_info()->has_thrown() &&
wrap->object()->Get(env->context(),
env->owner_string()).ToLocalChecked()
->IsUndefined());
}

static void Now(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
args.GetReturnValue().Set(GetNow(env));
}

static Local<Value> GetNow(Environment* env) {
uv_update_time(env->event_loop());
uint64_t now = uv_now(env->event_loop());
CHECK(now >= env->timer_base());
now -= env->timer_base();
if (now <= 0xfffffff)
args.GetReturnValue().Set(static_cast<uint32_t>(now));
return Integer::New(env->isolate(), static_cast<uint32_t>(now));
else
args.GetReturnValue().Set(static_cast<double>(now));
return Number::New(env->isolate(), static_cast<double>(now));
}

uv_timer_t handle_;
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-timers-unref-throw-then-ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const common = require('../common');
const assert = require('assert');

process.once('uncaughtException', common.expectsError({
message: 'Timeout Error'
}));

let called = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common.mustCall() doesn't work here? It checks for one and only one call by default.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can potentially cause an infinite loop since it only checks at exit and this tests the fact that calling ref on unref'd handle doesn't cause the C++ loop to continue forever.

This is related to the owner check in C++. Someone could, in the future, think to optimize that by checking whether the handle is ref or unref, instead of checking for presence of an owner, but that would actually be incorrect and would cause an infinite loop on this test.

const t = setTimeout(() => {
assert(!called);
called = true;
t.ref();
throw new Error('Timeout Error');
}, 1).unref();

setTimeout(common.mustCall(), 1);
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ const t = setInterval(() => {
cntr++;
if (cntr === 1) {
common.busyLoop(100);
// ensure that the event loop passes before the second interval
setImmediate(() => assert.strictEqual(cntr, 1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these new callbacks also have common.mustCall()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted not to include since setImmediate is guaranteed to run and if it doesn't, others tests will fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm yes but I'm not sure that's guaranteed to be true for all APIs so we do need to be careful about that path of doing things,

first = Timer.now();
} else if (cntr === 2) {
assert(Timer.now() - first < 100);
clearInterval(t);
}
}, 100);
const t2 = setInterval(() => {
if (cntr === 2) {
clearInterval(t2);
}
}, 100);