Skip to content

Commit c37a50a

Browse files
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.
1 parent cd9898a commit c37a50a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/internal/timers.js

Lines changed: 1 addition & 1 deletion
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;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Flags: --expose-internals
2+
3+
'use strict';
4+
5+
const common = require('../common');
6+
7+
// This test checks whether a refresh called inside the callback will keep
8+
// the event loop alive to run the timer again.
9+
10+
let didCall = false;
11+
const timer = setTimeout(common.mustCall(() => {
12+
if (!didCall) {
13+
didCall = true;
14+
timer.refresh();
15+
}
16+
}, 2), 1);

0 commit comments

Comments
 (0)