Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timers: fix refreshed timers exiting too early #26672

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function Timeout(callback, after, args, isRepeat) {
this._timerArgs = args;
this._repeat = isRepeat ? after : null;
this._destroyed = false;
this._refreshed = false;

this[kRefed] = null;

Expand All @@ -98,7 +99,7 @@ Timeout.prototype.refresh = function() {
getTimers().active(this);
else
getTimers()._unrefActive(this);

this._refreshed = true;
return this;
};

Expand Down
3 changes: 2 additions & 1 deletion lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,9 @@ function listOnTimeout(list, now) {
start = getLibuvNow();
insert(timer, timer[kRefed], start);
} else {
if (timer[kRefed])
if (timer[kRefed] && !timer._refreshed)
refCount--;
timer._refreshed = false;
timer[kRefed] = null;

if (destroyHooksExist() && !timer._destroyed) {
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-timers-refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ const { setUnrefTimeout } = require('internal/timers');

strictEqual(timer.refresh(), timer);
}

// refresh timeout in callback
{
let called = 0;
const timer = setTimeout(common.mustCall(() => {
called += 1;
if (called === 2) {
clearTimeout(timer);
}
timer.refresh();
}, 2), 1);
}