Skip to content

Commit

Permalink
timers: fix processing of nested same delay timers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
whitlockjc committed Jul 24, 2015
1 parent e192f61 commit 3964aed
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,20 @@ function listOnTimeout() {

var first;
while (first = L.peek(list)) {
// If the previous iteration caused a timer to be added,
// update the value of "now" so that timing computations are
// done correctly. See test/simple/test-timers-blocking-callback.js
// for more information.
// Whenever a timer creates a new timer in the current list being processed
// you can end up processing the new timer immediately instead of waiting
// the scheduled time. Whenever we encounter this situation stop
// processing this list and reschedule.
//
// See: https:/joyent/node/issues/25607
if (now < first._monotonicStartTime) {
now = Timer.now();
debug('now: %d', now);
var delay = msecs - (Timer.now() - first._monotonicStartTime);
if (delay < 0) {
delay = 0;
}
debug(msecs + ' list wait because timer was added from another timer');
list.start(delay, 0);
return;
}

var diff = now - first._monotonicStartTime;
Expand Down

0 comments on commit 3964aed

Please sign in to comment.