Skip to content
This repository was archived by the owner on Oct 16, 2021. It is now read-only.

Commit fbb2a44

Browse files
Julien GillijBarz
Julien Gilli
authored andcommitted
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 nodejs#9333 and nodejs#15447. PR: nodejs#17203 PR-URL: nodejs#17203 Reviewed-By: Fedor Indutny <[email protected]>
1 parent 7811c5e commit fbb2a44

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
* It makes sure that it works when the timers list for a given timeout is
40+
* empty (see testAddingTimerToEmptyTimersList) and when the timers list
41+
* is not empty (see testAddingTimerToNonEmptyTimersList).
42+
*/
43+
44+
var assert = require('assert');
45+
var common = require('../common');
46+
47+
var TIMEOUT = 100;
48+
49+
var nbBlockingCallbackCalls = 0;
50+
var latestDelay = 0;
51+
var timeCallbackScheduled = 0;
52+
53+
function initTest() {
54+
nbBlockingCallbackCalls = 0;
55+
latestDelay = 0;
56+
timeCallbackScheduled = 0;
57+
}
58+
59+
function blockingCallback(callback) {
60+
++nbBlockingCallbackCalls;
61+
62+
if (nbBlockingCallbackCalls > 1) {
63+
latestDelay = new Date().getTime() - timeCallbackScheduled;
64+
// Even if timers can fire later than when they've been scheduled
65+
// to fire, they should more than 50% later with a timeout of
66+
// 100ms. Firing later than that would mean that we hit the regression
67+
// highlighted in
68+
// https://github.com/joyent/node/issues/15447 and
69+
// https://github.com/joyent/node/issues/9333.
70+
assert(latestDelay < TIMEOUT * 1.5);
71+
if (callback)
72+
return callback();
73+
} else {
74+
// block by busy-looping to trigger the issue
75+
common.busyLoop(TIMEOUT);
76+
77+
timeCallbackScheduled = new Date().getTime();
78+
setTimeout(blockingCallback, TIMEOUT);
79+
}
80+
}
81+
82+
function testAddingTimerToEmptyTimersList(callback) {
83+
initTest();
84+
// Call setTimeout just once to make sure the timers list is
85+
// empty when blockingCallback is called.
86+
setTimeout(blockingCallback.bind(global, callback), TIMEOUT);
87+
}
88+
89+
function testAddingTimerToNonEmptyTimersList() {
90+
initTest();
91+
// Call setTimeout twice with the same timeout to make
92+
// sure the timers list is not empty when blockingCallback is called.
93+
setTimeout(blockingCallback, TIMEOUT);
94+
setTimeout(blockingCallback, TIMEOUT);
95+
}
96+
97+
// Run the test for the empty timers list case, and then for the non-empty
98+
// timers list one
99+
testAddingTimerToEmptyTimersList(testAddingTimerToNonEmptyTimersList);

0 commit comments

Comments
 (0)