Skip to content

Commit

Permalink
timers: fix refresh inside callback
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
  • Loading branch information
apapirovski authored and targos committed Mar 27, 2019
1 parent 899de0a commit 3ec652a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-timers-refresh-in-callback.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 3ec652a

Please sign in to comment.