Skip to content

Commit 3ec652a

Browse files
apapirovskitargos
authored andcommitted
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: #26642 Reviewed-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>
1 parent 899de0a commit 3ec652a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/internal/timers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ function getTimerCallbacks(runNextTicks) {
469469
if (start === undefined)
470470
start = getLibuvNow();
471471
insert(timer, timer[kRefed], start);
472-
} else {
472+
} else if (!timer._idleNext && !timer._idlePrev) {
473473
if (timer[kRefed])
474474
refCount--;
475475
timer[kRefed] = null;
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)