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

events: remove listener after timer is finished #35992

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
13 changes: 10 additions & 3 deletions lib/timers/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,24 @@ function setTimeout(after, value, options = {}) {
lazyDOMException('The operation was aborted', 'AbortError'));
}
return new Promise((resolve, reject) => {
const timeout = new Timeout(resolve, after, args, false, true);
let signalHandler = null;
function cleanupSignalHandlerThenResolve(arg) {
signal.removeEventListener('abort', signalHandler, { once: true });
resolve(arg);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to not allocate a closure here. This adds overhead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have to capture the handler in a closure since you need a reference to it to remove it.

const onDone = signal ? cleanupSignalHandlerThenResolve : resolve;
const timeout = new Timeout(onDone, after, args, false, true);
if (!ref) timeout.unref();
insert(timeout, timeout._idleTimeout);
if (signal) {
signal.addEventListener('abort', () => {
signalHandler = () => {
if (!timeout._destroyed) {
// eslint-disable-next-line no-undef
clearTimeout(timeout);
reject(lazyDOMException('The operation was aborted', 'AbortError'));
}
}, { once: true });
};
signal.addEventListener('abort', signalHandler, { once: true });
}
});
}
Expand Down
13 changes: 12 additions & 1 deletion test/parallel/test-timers-promisified.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Flags: --no-warnings
// Flags: --no-warnings --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const timers = require('timers');
const { promisify } = require('util');
const child_process = require('child_process');

// TODO(benjamingr) - refactor to use getEventListeners when #35991 lands
const { NodeEventTarget } = require('internal/event_target');

const timerPromises = require('timers/promises');

/* eslint-disable no-restricted-syntax */
Expand Down Expand Up @@ -91,6 +94,14 @@ process.on('multipleResolves', common.mustNotCall());
ac.abort();
});
}
{
// Check that timer adding signals does not leak handlers
const abortEmitter = new NodeEventTarget();
abortEmitter.aborted = false;
setTimeout(0, null, { signal: abortEmitter }).then(common.mustCall(() => {
assert.strictEqual(abortEmitter.listenerCount('abort'), 0);
}));
}

{
Promise.all(
Expand Down