Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit ae1a6ff

Browse files
author
Julien Gilli
committed
timers: do not fire timer if unenrolled
The previous commits introduced a behavior change. If two timers fired at the same time, and the first one to be processed in the list would unenroll the next one, the next one would still fire. This change puts back the old behavior of not firing the next timer to be processed in the list if it has been unenrolled.
1 parent f702d99 commit ae1a6ff

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

lib/timers.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,15 @@ var unrefList, unrefTimer;
406406

407407
function _makeTimerTimeout(timer) {
408408
var domain = timer.domain;
409+
var msecs = timer._idleTimeout;
409410

410-
if (!timer._onTimeout) return;
411+
// Timer has been unenrolled by another timer that fired at the same time,
412+
// so don't make it timeout.
413+
if (!msecs || msecs < 0)
414+
return;
415+
416+
if (!timer._onTimeout)
417+
return;
411418

412419
if (domain && domain._disposed)
413420
return;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
/*
23+
* The goal of this test is to make sure that, after the regression introduced
24+
* by 934bfe23a16556d05bfb1844ef4d53e8c9887c3d, the fix preserves the following
25+
* behavior of unref timers: if two timers are scheduled to fire at the same
26+
* time, if one unenrolls the other one in its _onTimeout callback, the other
27+
* one will *not* fire.
28+
*/
29+
var timers = require('timers');
30+
var assert = require('assert');
31+
32+
var nbTimersFired = 0;
33+
34+
var foo = new function() {
35+
this._onTimeout = function() {
36+
++nbTimersFired;
37+
timers.unenroll(bar);
38+
};
39+
}();
40+
41+
var bar = new function() {
42+
this._onTimeout = function() {
43+
++nbTimersFired;
44+
timers.unenroll(foo);
45+
};
46+
}();
47+
48+
timers.enroll(bar, 1);
49+
timers._unrefActive(bar);
50+
51+
timers.enroll(foo, 1);
52+
timers._unrefActive(foo);
53+
54+
setTimeout(function() {
55+
assert.notEqual(nbTimersFired, 2);
56+
}, 20);

0 commit comments

Comments
 (0)