Skip to content

Commit a3d58db

Browse files
zhangzifa穆客
authored and
穆客
committed
When close is called in timer(set by setInterval) callback, active and
listOnTimeout will be called again which should not be called anymore. When unenroll is called in timer(set by setInterval) callback, the timer still works. With this fix, the timer behaves the same as clearTimeout/clearInterval is called in callback.
1 parent 14f81a0 commit a3d58db

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

lib/timers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ exports.setInterval = function(callback, repeat) {
276276
timer._repeat();
277277

278278
// Timer might be closed - no point in restarting it
279-
if (!timer._repeat)
279+
if (timer._idleTimeout === -1)
280280
return;
281281

282282
// If timer is unref'd (or was - it's permanently removed from the list.)
@@ -352,6 +352,7 @@ Timeout.prototype.ref = function() {
352352
Timeout.prototype.close = function() {
353353
this._onTimeout = null;
354354
if (this._handle) {
355+
this._idleTimeout = -1;
355356
this._handle[kOnTimeout] = null;
356357
this._handle.close();
357358
} else {

test/fixtures/test-timer-disarm.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const T = require('timers');
5+
6+
// This test is to make sure the timer will not fire again
7+
// when it is disarmed in callback.
8+
// This script is called by
9+
// ../parallel/test-timers-disarmed-in-callbak-not-fire-anymore.js
10+
// with NODE_DEBUG=timer to make sure 'active' and 'listOnTimeout'
11+
// will not be called again after the timer is disarmed.
12+
// Before fix https://github.com/nodejs/node/pull/4303,
13+
// When disarm with clearInterval, it works fine.
14+
// When disarm with close, active and listOnTimeout will be called again.
15+
// When disarm with unenroll, timer still works.
16+
// After this fix, timer behaves the same as clearTimeout/clearInterval
17+
// when it is disared by close/unenroll in callback.
18+
19+
var nbIntervalFired1 = 0;
20+
var nbIntervalFired2 = 0;
21+
var nbIntervalFired3 = 0;
22+
const timer1 = setInterval(() => {
23+
nbIntervalFired1++;
24+
clearInterval(timer1);
25+
}, 11);
26+
const timer2 = setInterval(() => {
27+
nbIntervalFired2++;
28+
timer2.close();
29+
}, 12);
30+
const timer3 = setInterval(() => {
31+
nbIntervalFired3++;
32+
T.unenroll(timer3);
33+
}, 13);
34+
setTimeout(() => {
35+
assert.strictEqual(nbIntervalFired1, 1);
36+
assert.strictEqual(nbIntervalFired2, 1);
37+
assert.strictEqual(nbIntervalFired3, 1);
38+
}, 100);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const exec = require('child_process').exec;
5+
6+
const testPath = './test/fixtures/test-timer-disarm.js';
7+
const testCommand = 'NODE_DEBUG=timer ' + process.execPath + ' ' + testPath;
8+
9+
setTimeout(() => {
10+
exec(testCommand, (err, stdout, stderr) => {
11+
const o = stderr.split('\n');
12+
let timer1Fired = 0;
13+
let timer2Fired = 0;
14+
let timer3Fired = 0;
15+
for (var i = 0; i < o.length; i++) {
16+
var line = o[i];
17+
if (line.indexOf('timeout callback 11') >= 0) {
18+
timer1Fired++;
19+
}
20+
if (line.indexOf('timeout callback 12') >= 0) {
21+
timer2Fired++;
22+
}
23+
if (line.indexOf('timeout callback 13') >= 0) {
24+
timer3Fired++;
25+
}
26+
}
27+
assert.strictEqual(timer1Fired, 1);
28+
assert.strictEqual(timer2Fired, 1);
29+
assert.strictEqual(timer3Fired, 1);
30+
});
31+
}, 100);

0 commit comments

Comments
 (0)