Skip to content

Commit a794864

Browse files
committed
timers: fix processing of nested same delay timers
Whenever a timer of a specific delay creates a new timer with the same delay we currently end up processing the new timer immediately instead of scheduling the timer to run in the future. This commit basically identifies the situation, halts processing the current timer chain and reschedules the timer chain appropriately in the future. Fixes nodejs#25607
1 parent e192f61 commit a794864

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/timers.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,18 @@ 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.
90+
// Whenever a timer creates a new timer in the current list being processed
91+
// you can end up processing the new timer immediately instead of waiting
92+
// the scheduled time. Whenever we encounter this situation stop
93+
// processing this list and reschedule. (Issue 25607)
9494
if (now < first._monotonicStartTime) {
95-
now = Timer.now();
96-
debug('now: %d', now);
95+
var delay = msecs - (Timer.now() - first._monotonicStartTime);
96+
if (delay < 0) {
97+
delay = 0;
98+
}
99+
debug(msecs + ' list wait because timer was added from another timer');
100+
list.start(delay, 0);
101+
return;
97102
}
98103

99104
var diff = now - first._monotonicStartTime;

0 commit comments

Comments
 (0)