Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
timers: handle unrefed intervals in clearTimeout()
This commit clears the _repeat property of all timer objects in
clearTimeout(). This prevents intervals passed to clearTimeout()
from being rearmed.
  • Loading branch information
cjihrig committed Nov 14, 2016
commit 1b8efe645186bc59a94f6bf82a22a7481a3a4abc
2 changes: 1 addition & 1 deletion lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ function rearm(timer) {

const clearTimeout = exports.clearTimeout = function(timer) {
if (timer && (timer[kOnTimeout] || timer._onTimeout)) {
timer._repeat = null;
timer[kOnTimeout] = timer._onTimeout = null;
if (timer instanceof Timeout) {
timer.close(); // for after === 0
Expand Down Expand Up @@ -444,7 +445,6 @@ function createRepeatTimeout(callback, repeat, args) {

exports.clearInterval = function(timer) {
if (timer && timer._repeat) {
timer._repeat = null;
clearTimeout(timer);
}
};
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-timers-cleartimeout-unref-interval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
const common = require('../common');
const timeout = setTimeout(common.fail, 2 ** 30); // Keep event loop open.
const interval = setInterval(common.mustCall(() => {
// Use clearTimeout() instead of clearInterval().
// The interval should still be cleared.
clearTimeout(interval);
setImmediate(() => { clearTimeout(timeout); });
}), 1).unref();