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

Fix timer .unref() handle management once and for all #1330

Closed
wants to merge 5 commits into from
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
27 changes: 17 additions & 10 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,8 @@ exports.enroll = function(item, msecs) {
// it will reset its timeout.
exports.active = function(item) {
var msecs = item._idleTimeout;
if (msecs >= 0) {
var list = lists[msecs];
if (!list || L.isEmpty(list)) {
insert(item, msecs);
} else {
item._idleStart = Timer.now();
L.append(list, item);
}
}
if (msecs >= 0)
insert(item, msecs);
};


Expand Down Expand Up @@ -272,6 +265,11 @@ exports.setInterval = function(callback, repeat) {

function wrapper() {
timer._repeat.call(this);

// Timer might be closed - no point in restarting it
if (!timer._repeat)
return;

// If timer is unref'd (or was - it's permanently removed from the list.)
if (this._handle) {
this._handle.start(repeat, 0);
Expand Down Expand Up @@ -301,6 +299,14 @@ const Timeout = function(after) {
this._repeat = null;
};


function unrefdHandle() {
this.owner._onTimeout();
if (!this.owner._repeat)
this.owner.close();
Copy link
Contributor

Choose a reason for hiding this comment

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

This will run this._handle.close() which runs uv_close(wrap->handle__, OnClose). Are we sure that the list of timers attached to this TimerWrap instance is empty before this runs?

Copy link
Member Author

Choose a reason for hiding this comment

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

No list attached here for sure.

Copy link
Contributor

Choose a reason for hiding this comment

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

great. then LGTM.

}


Timeout.prototype.unref = function() {
if (this._handle) {
this._handle.unref();
Expand All @@ -315,7 +321,8 @@ Timeout.prototype.unref = function() {
if (this._called && !this._repeat) return;

this._handle = new Timer();
this._handle[kOnTimeout] = this._onTimeout;
this._handle.owner = this;
this._handle[kOnTimeout] = unrefdHandle;
this._handle.start(delay, 0);
this._handle.domain = this.domain;
this._handle.unref();
Expand Down
10 changes: 10 additions & 0 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class TimerWrap : public HandleWrap {
static void Start(const FunctionCallbackInfo<Value>& args) {
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

CHECK(HandleWrap::IsAlive(wrap));

int64_t timeout = args[0]->IntegerValue();
int64_t repeat = args[1]->IntegerValue();
int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, repeat);
Expand All @@ -82,20 +84,26 @@ class TimerWrap : public HandleWrap {
static void Stop(const FunctionCallbackInfo<Value>& args) {
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

CHECK(HandleWrap::IsAlive(wrap));

int err = uv_timer_stop(&wrap->handle_);
args.GetReturnValue().Set(err);
}

static void Again(const FunctionCallbackInfo<Value>& args) {
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

CHECK(HandleWrap::IsAlive(wrap));

int err = uv_timer_again(&wrap->handle_);
args.GetReturnValue().Set(err);
}

static void SetRepeat(const FunctionCallbackInfo<Value>& args) {
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

CHECK(HandleWrap::IsAlive(wrap));

int64_t repeat = args[0]->IntegerValue();
uv_timer_set_repeat(&wrap->handle_, repeat);
args.GetReturnValue().Set(0);
Expand All @@ -104,6 +112,8 @@ class TimerWrap : public HandleWrap {
static void GetRepeat(const FunctionCallbackInfo<Value>& args) {
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

CHECK(HandleWrap::IsAlive(wrap));

int64_t repeat = uv_timer_get_repeat(&wrap->handle_);
if (repeat <= 0xfffffff)
args.GetReturnValue().Set(static_cast<uint32_t>(repeat));
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-timers-unref-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var assert = require('assert');

var called = 0;
var closed = 0;

var timeout = setTimeout(function() {
called++;
}, 10);
timeout.unref();

// Wrap `close` method to check if the handle was closed
var close = timeout._handle.close;
timeout._handle.close = function() {
closed++;
return close.apply(this, arguments);
};

// Just to keep process alive and let previous timer's handle die
setTimeout(function() {
}, 50);

process.on('exit', function() {
assert.equal(called, 1);
assert.equal(closed, 1);
});
18 changes: 18 additions & 0 deletions test/parallel/test-timers-unrefd-interval-still-fires.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* This test is a regression test for joyent/node#8900.
*/
var assert = require('assert');

var N = 5;
var nbIntervalFired = 0;
var timer = setInterval(function() {
++nbIntervalFired;
if (nbIntervalFired === N)
clearInterval(timer);
}, 1);

timer.unref();

setTimeout(function onTimeout() {
assert.strictEqual(nbIntervalFired, N);
}, 100);