Skip to content

Commit

Permalink
timers: assure setTimeout callback only runs once
Browse files Browse the repository at this point in the history
This fixes an edge case where running this.unref() during the callback caused the
callback to get executed multiple times.
  • Loading branch information
silverwind committed Mar 21, 2015
1 parent 99c79f8 commit 951168f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function listOnTimeout() {
if (domain)
domain.enter();
threw = true;
first._called = true;
first._onTimeout();
if (domain)
domain.exit();
Expand Down Expand Up @@ -305,6 +306,9 @@ Timeout.prototype.unref = function() {
if (this._handle) {
this._handle.unref();
} else if (typeof(this._onTimeout) === 'function') {
// Prevent running callback multiple times
// when unref() is called during the callback
if (this._called) return;
var now = Timer.now();
if (!this._idleStart) this._idleStart = now;
var delay = this._idleStart + this._idleTimeout - now;
Expand Down Expand Up @@ -492,6 +496,7 @@ function unrefTimeout() {
if (domain) domain.enter();
threw = true;
debug('unreftimer firing timeout');
first._called = true;
first._onTimeout();
threw = false;
if (domain)
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-timers-unref.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var interval_fired = false,
timeout_fired = false,
unref_interval = false,
unref_timer = false,
unref_callbacks = 0,
interval, check_unref, checks = 0;

var LONG_TIME = 10 * 1000;
Expand Down Expand Up @@ -34,6 +35,11 @@ check_unref = setInterval(function() {
checks += 1;
}, 100);

setTimeout(function() {
unref_callbacks++;
this.unref();
}, SHORT_TIME);

// Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261.
(function() {
var t = setInterval(function() {}, 1);
Expand All @@ -46,4 +52,5 @@ process.on('exit', function() {
assert.strictEqual(timeout_fired, false, 'Timeout should not fire');
assert.strictEqual(unref_timer, true, 'An unrefd timeout should still fire');
assert.strictEqual(unref_interval, true, 'An unrefd interval should still fire');
assert.strictEqual(unref_callbacks, 1, 'Callback should only run once');
});

0 comments on commit 951168f

Please sign in to comment.