diff --git a/lib/internal/timers.js b/lib/internal/timers.js index 074ad7d556..b44952cafe 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 0000000000..df62512acd --- /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);