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`.

Fixes: nodejs#9084
  • Loading branch information
mscdex committed Oct 13, 2016
1 parent 804d57d commit 6750abf
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 6750abf

Please sign in to comment.