From d2e9b76c1df69dcda93544abb9b2c9e10adcbb82 Mon Sep 17 00:00:00 2001 From: Suguru Motegi Date: Tue, 6 Nov 2018 21:44:31 -0800 Subject: [PATCH] timers: fix setTimeout expiration logic Fix the timer logic to be the same as v10.30.0. Fixes: https://github.com/nodejs/node/issues/24203 PR-URL: https://github.com/nodejs/node/pull/24214 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Gus Caplan Reviewed-By: Anatoli Papirovski Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- lib/timers.js | 2 +- .../test-timers-timeout-with-non-integer.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-timers-timeout-with-non-integer.js diff --git a/lib/timers.js b/lib/timers.js index 575dcf25f6e481..1531cd1fb60726 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -283,7 +283,7 @@ function listOnTimeout(list, now) { // Check if this loop iteration is too early for the next timer. // This happens if there are more timers scheduled for later in the list. if (diff < msecs) { - list.expiry = timer._idleStart + msecs; + list.expiry = Math.max(timer._idleStart + msecs, now + 1); list.id = timerListId++; queue.percolateDown(1); debug('%d list wait because diff is %d', msecs, diff); diff --git a/test/parallel/test-timers-timeout-with-non-integer.js b/test/parallel/test-timers-timeout-with-non-integer.js new file mode 100644 index 00000000000000..96efc69e5096fd --- /dev/null +++ b/test/parallel/test-timers-timeout-with-non-integer.js @@ -0,0 +1,15 @@ +'use strict'; +const common = require('../common'); + +/** + * This test is for https://github.com/nodejs/node/issues/24203 + */ +let count = 50; +const time = 1.00000000000001; +const exec = common.mustCall(() => { + if (--count === 0) { + return; + } + setTimeout(exec, time); +}, count); +exec();