From 785a03ae40186bb89082504fb4fd76a9a7bc2d6b Mon Sep 17 00:00:00 2001 From: Chris Dickinson Date: Sun, 3 Aug 2014 17:48:31 -0700 Subject: [PATCH 1/3] timers: float delays can cause 100% cpu usage. The changes in befbbad switch all comparisons over to unsigned integer comparisons, but in certain cases list.msecs will be fractional. If this is the case, the timer will never be removed from the list. This fixes the issue by casting msecs to an integer by rounding up or down. Fixes 8065. --- lib/timers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/timers.js b/lib/timers.js index be39ea66def..7d5e752c24b 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -68,7 +68,7 @@ function insert(item, msecs) { L.init(list); lists[msecs] = list; - list.msecs = msecs; + list.msecs = Math.round(msecs); list.ontimeout = listOnTimeout; } From 9ca03435b0af0711223d8daff8939a718c7768f6 Mon Sep 17 00:00:00 2001 From: Chris Dickinson Date: Sun, 3 Aug 2014 22:20:42 -0700 Subject: [PATCH 2/3] move coercion further up --- lib/timers.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/timers.js b/lib/timers.js index 7d5e752c24b..7963c499306 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -53,6 +53,9 @@ function insert(item, msecs) { item._idleStart = Date.now(); item._monotonicStartTime = Timer.now(); + // fractional delay values are not allowed; (0, 1) range + // should be coerced to 1ms. + msecs = Math.ceil(msecs); item._idleTimeout = msecs; if (msecs < 0) return; @@ -68,7 +71,7 @@ function insert(item, msecs) { L.init(list); lists[msecs] = list; - list.msecs = Math.round(msecs); + list.msecs = msecs; list.ontimeout = listOnTimeout; } From fa479bd44511a5a3ba21245fd8faba2ae1784f1d Mon Sep 17 00:00:00 2001 From: Chris Dickinson Date: Sun, 3 Aug 2014 22:34:33 -0700 Subject: [PATCH 3/3] add (probably insufficient) test --- .../test-timers-fractional-settimeout.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 test/simple/test-timers-fractional-settimeout.js diff --git a/test/simple/test-timers-fractional-settimeout.js b/test/simple/test-timers-fractional-settimeout.js new file mode 100644 index 00000000000..09d25fd37e4 --- /dev/null +++ b/test/simple/test-timers-fractional-settimeout.js @@ -0,0 +1,21 @@ +var times = 0; + +setInterval(function() { + process.stderr.write('.') +}, 0); + +setTimeout(function() { + process.exit(times === 3 ? 0 : 1); +}, 102); + +var a = setTimeout(function() { + ++times; +}, 100.3); + +var b = setTimeout(function() { + ++times; +}, 4.3); + +var c = setTimeout(function() { + ++times; +}, 100);