Skip to content

Commit

Permalink
timers: prevent event loop blocking
Browse files Browse the repository at this point in the history
When an interval takes as long or longer to run as its timeout setting
and the roundtrip from rearm() to its deferal takes exactly 1ms, that
interval can then block the event loop. This is an edge case of another
recently fixed bug (which in itself was an edge case).

PR-URL: nodejs#18486
Refs: nodejs#15072
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
apapirovski authored and MayaLekova committed May 8, 2018
1 parent 52d2876 commit 2e8be1d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ TimerWrap.prototype[kOnTimeout] = function listOnTimeout(now) {
// This happens if there are more timers scheduled for later in the list.
if (diff < msecs) {
var timeRemaining = msecs - (TimerWrap.now() - timer._idleStart);
if (timeRemaining < 0) {
if (timeRemaining <= 0) {
timeRemaining = 1;
}
this.start(timeRemaining);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ const t = setInterval(() => {
cntr++;
if (cntr === 1) {
common.busyLoop(100);
// ensure that the event loop passes before the second interval
setImmediate(() => assert.strictEqual(cntr, 1));
first = Timer.now();
} else if (cntr === 2) {
assert(Timer.now() - first < 100);
clearInterval(t);
}
}, 100);
const t2 = setInterval(() => {
if (cntr === 2) {
clearInterval(t2);
}
}, 100);

0 comments on commit 2e8be1d

Please sign in to comment.