From 3605726aaf03059655a6f8c347ef50ecf36e6cb2 Mon Sep 17 00:00:00 2001 From: hveldstra Date: Wed, 23 Nov 2016 10:57:49 +0000 Subject: [PATCH] timers: fix handling of cleared immediates If current immediate has no callback, move on to the next one in the queue. Fixes: https://github.com/nodejs/node/issues/9756 PR-URL: https://github.com/nodejs/node/pull/9759 Reviewed-By: Jeremiah Senkpiel --- lib/timers.js | 4 +++- test/parallel/test-timers-regress-GH-9765.js | 23 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-timers-regress-GH-9765.js diff --git a/lib/timers.js b/lib/timers.js index a55e265f29ffe9..6d456da36faf67 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -580,8 +580,10 @@ function processImmediate() { while (immediate) { domain = immediate.domain; - if (!immediate._onImmediate) + if (!immediate._onImmediate) { + immediate = immediate._idleNext; continue; + } if (domain) domain.enter(); diff --git a/test/parallel/test-timers-regress-GH-9765.js b/test/parallel/test-timers-regress-GH-9765.js new file mode 100644 index 00000000000000..71169fa0d1704c --- /dev/null +++ b/test/parallel/test-timers-regress-GH-9765.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../common'); + +// This test ensures that if an Immediate callback clears subsequent +// immediates we don't get stuck in an infinite loop. +// +// If the process does get stuck, it will be timed out by the test +// runner. +// +// Ref: https://github.com/nodejs/node/issues/9756 + +setImmediate(common.mustCall(function() { + clearImmediate(i2); + clearImmediate(i3); +})); + +const i2 = setImmediate(function() { + common.fail('immediate callback should not have fired'); +}); + +const i3 = setImmediate(function() { + common.fail('immediate callback should not have fired'); +});