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

Commit 4d34651

Browse files
author
Julien Gilli
committed
timers: fix timeout when added in timer's callback
When a timer is added in another timer's callback, its underlying timer handle will be started with a timeout that is actually incorrect. The reason is that the value that represents the current time is not updated between the time the original callback is called and the time the added timer is processed by timers.listOnTimeout. That leads the logic in timers.listOnTimeout to do an incorrect computation that makes the added timer fire with a timeout of scheduledTimeout + timeSpentInCallback. This change fixes that and make timers scheduled within other timers' callbacks fire as expected. Fixes #9333 and #15447.
1 parent 9800e0b commit 4d34651

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

lib/timers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ function listOnTimeout() {
8282

8383
debug('timeout callback ' + msecs);
8484

85-
var now = Timer.now();
86-
debug('now: ' + now);
87-
8885
var first;
8986
while (first = L.peek(list)) {
87+
var now = Timer.now();
88+
debug('now: ' + now);
89+
9090
var diff = now - first._monotonicStartTime;
9191
if (diff < msecs) {
9292
list.start(msecs - diff, 0);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
* This is a regression test for https://github.com/joyent/node/issues/15447
24+
* and https://github.com/joyent/node/issues/9333.
25+
*
26+
* When a timer is added in another timer's callback, its underlying timer
27+
* handle was started with a timeout that was actually incorrect.
28+
*
29+
* The reason was that the value that represents the current time was not
30+
* updated between the time the original callback was called and the time
31+
* the added timer was processed by timers.listOnTimeout. That lead the
32+
* logic in timers.listOnTimeout to do an incorrect computation that made
33+
* the added timer fire with a timeout of scheduledTimeout +
34+
* timeSpentInCallback.
35+
*
36+
* This test makes sure that a timer added by another timer's callback
37+
* fire with the expected timeout.
38+
*/
39+
40+
var assert = require('assert');
41+
42+
var TIMEOUT = 100;
43+
44+
function busyLoop(time) {
45+
var startTime = new Date().getTime();
46+
var stopTime = startTime + time;
47+
while (new Date().getTime() < stopTime) {
48+
;
49+
}
50+
}
51+
52+
var nbRuns = 0;
53+
var latestDelay;
54+
var timeCallbackScheduled;
55+
56+
function blockingCallback() {
57+
++nbRuns;
58+
59+
if (nbRuns === 2) {
60+
latestDelay = new Date().getTime() - timeCallbackScheduled;
61+
// Even if timers can fire later than when they've been scheduled
62+
// to fire, they should more than 50% later with a timeout of
63+
// 100ms. Firing later than that would mean that we hit the regression
64+
// highlighted in
65+
// https://github.com/joyent/node/issues/15447 and
66+
// https://github.com/joyent/node/issues/9333.
67+
assert(latestDelay < TIMEOUT * 1.5);
68+
} else {
69+
// block by busy-looping to trigger the issue
70+
busyLoop(TIMEOUT);
71+
72+
timeCallbackScheduled = new Date().getTime();
73+
setTimeout(blockingCallback, TIMEOUT);
74+
}
75+
}
76+
77+
setTimeout(blockingCallback, TIMEOUT);

0 commit comments

Comments
 (0)