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 <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
  • Loading branch information
mscdex authored and evanlucas committed Oct 14, 2016
1 parent 8b90814 commit 1d8f068
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 1d8f068

Please sign in to comment.