diff --git a/lib/timers.js b/lib/timers.js index cb049b0333..5419c03870 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -43,6 +43,7 @@ function shift (list) { function remove (item) { item._idleNext._idlePrev = item._idlePrev; item._idlePrev._idleNext = item._idleNext; + item._idleNext = null; } @@ -111,8 +112,7 @@ function insert (item, msecs) { var unenroll = exports.unenroll = function (item) { if (item._idleNext) { - item._idleNext._idlePrev = item._idlePrev; - item._idlePrev._idleNext = item._idleNext; + remove(item); var list = lists[item._idleTimeout]; // if empty then stop the watcher diff --git a/test/pummel/test-timers.js b/test/pummel/test-timers.js index 5841444172..a9681d782a 100644 --- a/test/pummel/test-timers.js +++ b/test/pummel/test-timers.js @@ -84,8 +84,26 @@ interval4 = setInterval(function () { if (++count4 > 10) clearInterval(interval4); }, 0); + +// we should be able to clearTimeout multiple times without breakage. +var expectedTimeouts = 3; + +function t () { + expectedTimeouts--; +} + +w = setTimeout(t, 200), +x = setTimeout(t, 200), +y = setTimeout(t, 200); + +clearTimeout(y), +z = setTimeout(t, 200); +clearTimeout(y); + + process.addListener("exit", function () { assert.equal(true, setTimeout_called); assert.equal(3, interval_count); assert.equal(11, count4); + assert.equal(0, expectedTimeouts, "clearTimeout cleared too many timeouts"); });