From 568b6a5c9e9893fbb6ab811cfa76841fb3c80a0a Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Mon, 5 Feb 2018 09:50:18 -0500 Subject: [PATCH] timers: be more defensive with intervals It's possible for user-code to flip an existing timeout to be an interval during its execution, in which case the current code would crash due to start being undefined. Fix this by providing a default start value within rearm. PR-URL: https://github.com/nodejs/node/pull/18579 Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater --- lib/timers.js | 3 +-- test/parallel/test-timers-timeout-to-interval.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-timers-timeout-to-interval.js diff --git a/lib/timers.js b/lib/timers.js index 7b7fb4e04f5..4d41b5dc8a4 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -442,8 +442,7 @@ function ontimeout(timer, start) { rearm(timer, start); } - -function rearm(timer, start) { +function rearm(timer, start = TimerWrap.now()) { // // Do not re-arm unenroll'd or closed timers. if (timer._idleTimeout === -1) return; diff --git a/test/parallel/test-timers-timeout-to-interval.js b/test/parallel/test-timers-timeout-to-interval.js new file mode 100644 index 00000000000..6952f2231a7 --- /dev/null +++ b/test/parallel/test-timers-timeout-to-interval.js @@ -0,0 +1,12 @@ +'use strict'; +const common = require('../common'); + +// This isn't officially supported but nonetheless is something that is +// currently possible and as such it shouldn't cause the process to crash + +const t = setTimeout(common.mustCall(() => { + if (t._repeat) { + clearInterval(t); + } + t._repeat = true; +}, 2), 1);