From 4306300b5ea8d8c4ff3daf64c7ed5fd64055ec2f Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sun, 17 Mar 2019 11:26:01 -0700 Subject: [PATCH] timers: fix refresh inside callback When `timers.refresh()` is called inside a callback, the timer would incorrectly end up unrefed and thus not keep the event loop alive. PR-URL: https://github.com/nodejs/node/pull/26721 Fixes: https://github.com/nodejs/node/issues/26642 Reviewed-By: Ruben Bridgewater Reviewed-By: Richard Lau Reviewed-By: Weijia Wang Reviewed-By: Anto Aravinth Reviewed-By: Jeremiah Senkpiel --- lib/internal/timers.js | 2 +- test/parallel/test-timers-refresh-in-callback.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-timers-refresh-in-callback.js diff --git a/lib/internal/timers.js b/lib/internal/timers.js index 074ad7d5566fcc..b44952cafefe57 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -469,7 +469,7 @@ function getTimerCallbacks(runNextTicks) { if (start === undefined) start = getLibuvNow(); insert(timer, timer[kRefed], start); - } else { + } else if (!timer._idleNext && !timer._idlePrev) { if (timer[kRefed]) refCount--; timer[kRefed] = null; diff --git a/test/parallel/test-timers-refresh-in-callback.js b/test/parallel/test-timers-refresh-in-callback.js new file mode 100644 index 00000000000000..df62512acd60b1 --- /dev/null +++ b/test/parallel/test-timers-refresh-in-callback.js @@ -0,0 +1,14 @@ +'use strict'; + +const common = require('../common'); + +// This test checks whether a refresh called inside the callback will keep +// the event loop alive to run the timer again. + +let didCall = false; +const timer = setTimeout(common.mustCall(() => { + if (!didCall) { + didCall = true; + timer.refresh(); + } +}, 2), 1);