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 multipleResolves in promisified timeouts/immediates #33949

Closed
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
12 changes: 8 additions & 4 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ setTimeout[customPromisify] = function(after, value, options = {}) {
insert(timeout, timeout._idleTimeout);
if (signal) {
signal.addEventListener('abort', () => {
clearTimeout(timeout);
reject(lazyDOMException('AbortError'));
if (!timeout._destroyed) {
clearTimeout(timeout);
reject(lazyDOMException('AbortError'));
}
}, { once: true });
}
});
Expand Down Expand Up @@ -340,8 +342,10 @@ setImmediate[customPromisify] = function(value, options = {}) {
const immediate = new Immediate(resolve, [value]);
if (signal) {
signal.addEventListener('abort', () => {
clearImmediate(immediate);
reject(lazyDOMException('AbortError'));
if (!immediate._destroyed) {
clearImmediate(immediate);
reject(lazyDOMException('AbortError'));
}
}, { once: true });
}
});
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-timers-promisified.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const { promisify } = require('util');
const setTimeout = promisify(timers.setTimeout);
const setImmediate = promisify(timers.setImmediate);

process.on('multipleResolves', common.mustNotCall());

{
const promise = setTimeout(1);
promise.then(common.mustCall((value) => {
Expand Down Expand Up @@ -66,6 +68,23 @@ const setImmediate = promisify(timers.setImmediate);
assert.rejects(setImmediate(10, { signal }), /AbortError/);
}

{
// Check that aborting after resolve will not reject.
const ac = new AbortController();
const signal = ac.signal;
setTimeout(10, undefined, { signal }).then(() => {
ac.abort();
});
}
{
// Check that aborting after resolve will not reject.
const ac = new AbortController();
const signal = ac.signal;
setImmediate(10, { signal }).then(() => {
ac.abort();
});
}

{
Promise.all(
[1, '', false, Infinity].map((i) => assert.rejects(setImmediate(10, i)), {
Expand Down