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

Commit 976936c

Browse files
committed
timers: fix processing of nested same delay timers
Whenever a timer with a specific timeout value creates a new timer with the same timeout, the newly added timer might be processed immediately in the same tick of the event loop instead of during the next tick of the event loop at the earliest. Fixes #25607
1 parent e192f61 commit 976936c

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

lib/timers.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,35 @@ function listOnTimeout() {
8787

8888
var first;
8989
while (first = L.peek(list)) {
90-
// If the previous iteration caused a timer to be added,
91-
// update the value of "now" so that timing computations are
92-
// done correctly. See test/simple/test-timers-blocking-callback.js
93-
// for more information.
94-
if (now < first._monotonicStartTime) {
95-
now = Timer.now();
96-
debug('now: %d', now);
90+
// This handles the case of a timer that was created within a timers
91+
// callback with the same timeout value. For instance, when processing the
92+
// timer that would call `bar` in such code:
93+
//
94+
// setTimeout(function foo() { setTimeout(function bar() {}, 0) }, 0);
95+
//
96+
// or
97+
//
98+
// setTimeout(function foo() { setTimeout(function bar() {}, 500) }, 500);
99+
//
100+
// We want to make sure that newly added timer fires in the next turn of the
101+
// event loop at the earliest. So even if it's already expired now,
102+
// reschedule it to fire later.
103+
//
104+
// At that point, it's not necessary to process any other timer in that
105+
// list, because any remaining timer has been added within a callback of a
106+
// timer that has already been processed, and thus needs to be processed at
107+
// the earliest not in the current tick, but when the rescheduled timer will
108+
// expire.
109+
//
110+
// See: https://github.com/joyent/node/issues/25607
111+
if (now <= first._monotonicStartTime) {
112+
var timeRemaining = msecs - (Timer.now() - first._monotonicStartTime);
113+
if (timeRemaining < 0) {
114+
timeRemaining = 0;
115+
}
116+
debug(msecs + ' list wait because timer was added from another timer');
117+
list.start(timeRemaining, 0);
118+
return;
97119
}
98120

99121
var diff = now - first._monotonicStartTime;

test/common.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
var path = require('path');
2323
var fs = require('fs');
2424
var assert = require('assert');
25+
var Timer = process.binding('timer_wrap').Timer;
2526

2627
exports.testDir = path.dirname(__filename);
2728
exports.fixturesDir = path.join(exports.testDir, 'fixtures');
@@ -220,9 +221,9 @@ exports.hasMultiLocalhost = function hasMultiLocalhost() {
220221
};
221222

222223
exports.busyLoop = function busyLoop(time) {
223-
var startTime = new Date().getTime();
224+
var startTime = Timer.now();
224225
var stopTime = startTime + time;
225-
while (new Date().getTime() < stopTime) {
226+
while (Timer.now() < stopTime) {
226227
;
227228
}
228229
};

test/simple/test-timers-nested.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
// Make sure we test 0ms timers, since they would had always wanted to run on
26+
// the current tick, and greater than 0ms timers, for scenarios where the
27+
// outer timer takes longer to complete than the delay of the nested timer.
28+
// Since the process of recreating this is identical regardless of the timer
29+
// delay, these scenarios are in one test.
30+
var scenarios = [0, 100];
31+
32+
scenarios.forEach(function (delay) {
33+
var nestedCalled = false;
34+
35+
setTimeout(function A() {
36+
// Create the nested timer with the same delay as the outer timer so that it
37+
// gets added to the current list of timers being processed by listOnTimeout.
38+
setTimeout(function B() {
39+
nestedCalled = true;
40+
}, delay);
41+
42+
// Busy loop for the same timeout used for the nested timer to ensure that we
43+
// are in fact expiring the nested timer.
44+
common.busyLoop(delay);
45+
46+
process.nextTick(function() {
47+
assert.ok(!nestedCalled);
48+
});
49+
}, delay);
50+
});

0 commit comments

Comments
 (0)