timers: fix refresh inside callback · nodejs/node@3ec652a (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Commit 3ec652a

apapirovskitargos

authored and

committed

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: #26721 Fixes: #26642Reviewed-By: Ruben Bridgewater ruben@bridgewater.de Reviewed-By: Richard Lau riclau@uk.ibm.com Reviewed-By: Weijia Wang starkwang@126.com Reviewed-By: Anto Aravinth anto.aravinth.cse@gmail.com Reviewed-By: Jeremiah Senkpiel fishrock123@rocketmail.com

File tree

2 files changed

lines changed

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -469,7 +469,7 @@ function getTimerCallbacks(runNextTicks) {
469 469 if (start === undefined)
470 470 start = getLibuvNow();
471 471 insert(timer, timer[kRefed], start);
472 -} else {
472 +} else if (!timer._idleNext && !timer._idlePrev) {
473 473 if (timer[kRefed])
474 474 refCount--;
475 475 timer[kRefed] = null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1 +'use strict';
2 +
3 +const common = require('../common');
4 +
5 +// This test checks whether a refresh called inside the callback will keep
6 +// the event loop alive to run the timer again.
7 +
8 +let didCall = false;
9 +const timer = setTimeout(common.mustCall(() => {
10 +if (!didCall) {
11 +didCall = true;
12 +timer.refresh();
13 +}
14 +}, 2), 1);