Skip to content

Commit

Permalink
timers: fix regression with clearImmediate()
Browse files Browse the repository at this point in the history
This commit fixes a regression introduced in 0ed8839 that caused
additional queued immediate callbacks to be ignored if
`clearImmediate(immediate)` was called within the callback for
`immediate`.

PR-URL: #9086
Fixes: #9084
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
  • Loading branch information
mscdex authored and evanlucas committed Oct 14, 2016
1 parent 2540417 commit 7171bd6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,21 @@ function processImmediate() {
domain.enter();

immediate._callback = immediate._onImmediate;

// Save next in case `clearImmediate(immediate)` is called from callback
var next = immediate._idleNext;

tryOnImmediate(immediate, tail);

if (domain)
domain.exit();

immediate = immediate._idleNext;
// If `clearImmediate(immediate)` wasn't called from the callback, use the
// `immediate`'s next item
if (immediate._idleNext)
immediate = immediate._idleNext;
else
immediate = next;
}

// Only round-trip to C++ land if we have to. Calling clearImmediate() on an
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-timers-clearImmediate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
require('../common');
const assert = require('assert');

const N = 3;
var count = 0;
function next() {
const immediate = setImmediate(function() {
clearImmediate(immediate);
++count;
});
}
for (var i = 0; i < N; ++i)
next();

process.on('exit', () => {
assert.strictEqual(count, N, `Expected ${N} immediate callback executions`);
});

0 comments on commit 7171bd6

Please sign in to comment.