Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

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: nodejs/node#26721
Fixes: nodejs/node#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>
  • Loading branch information
apapirovski authored and danbev committed Mar 20, 2019
1 parent d933cc4 commit 4306300
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 4306300

Please sign in to comment.