Skip to content

Commit 6930205

Browse files
nbbeekenmarco-ippolito
authored andcommitted
events: remove abort listener from signal in on
the `abortHandler` function is declared within the scope of the `events.on` function so cannot be removed by the caller which can lead to a memory leak adding the abort listener using the `addAbortListener` helper returns a disposable that can be used to clean up the listener when the iterator is exited Fixes: #51010 PR-URL: #51091 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 5a92223 commit 6930205

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

lib/events.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1164,14 +1164,8 @@ function on(emitter, event, options = kEmptyObject) {
11641164
addEventListener(emitter, closeEvents[i], closeHandler);
11651165
}
11661166
}
1167-
if (signal) {
1168-
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
1169-
eventTargetAgnosticAddListener(
1170-
signal,
1171-
'abort',
1172-
abortListener,
1173-
{ __proto__: null, once: true, [kResistStopPropagation]: true });
1174-
}
1167+
1168+
const abortListenerDisposable = signal ? addAbortListener(signal, abortListener) : null;
11751169

11761170
return iterator;
11771171

@@ -1198,6 +1192,7 @@ function on(emitter, event, options = kEmptyObject) {
11981192
}
11991193

12001194
function closeHandler() {
1195+
abortListenerDisposable?.[SymbolDispose]();
12011196
removeAll();
12021197
finished = true;
12031198
const doneResult = createIterResult(undefined, true);

test/parallel/test-events-on-async-iterator.js

+32
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const assert = require('assert');
66
const { on, EventEmitter } = require('events');
77
const {
88
NodeEventTarget,
9+
kEvents
910
} = require('internal/event_target');
1011

1112
async function basic() {
@@ -363,6 +364,36 @@ async function abortableOnAfterDone() {
363364
});
364365
}
365366

367+
async function abortListenerRemovedAfterComplete() {
368+
const ee = new EventEmitter();
369+
const ac = new AbortController();
370+
371+
const i = setInterval(() => ee.emit('foo', 'foo'), 1);
372+
try {
373+
// Below: either the kEvents map is empty or the 'abort' listener list is empty
374+
375+
// Return case
376+
const endedIterator = on(ee, 'foo', { signal: ac.signal });
377+
assert.ok(ac.signal[kEvents].get('abort').size > 0);
378+
endedIterator.return();
379+
assert.strictEqual(ac.signal[kEvents].get('abort')?.size ?? ac.signal[kEvents].size, 0);
380+
381+
// Throw case
382+
const throwIterator = on(ee, 'foo', { signal: ac.signal });
383+
assert.ok(ac.signal[kEvents].get('abort').size > 0);
384+
throwIterator.throw(new Error());
385+
assert.strictEqual(ac.signal[kEvents].get('abort')?.size ?? ac.signal[kEvents].size, 0);
386+
387+
// Abort case
388+
on(ee, 'foo', { signal: ac.signal });
389+
assert.ok(ac.signal[kEvents].get('abort').size > 0);
390+
ac.abort(new Error());
391+
assert.strictEqual(ac.signal[kEvents].get('abort')?.size ?? ac.signal[kEvents].size, 0);
392+
} finally {
393+
clearInterval(i);
394+
}
395+
}
396+
366397
async function run() {
367398
const funcs = [
368399
basic,
@@ -382,6 +413,7 @@ async function run() {
382413
eventTargetAbortableOnAfter,
383414
eventTargetAbortableOnAfter2,
384415
abortableOnAfterDone,
416+
abortListenerRemovedAfterComplete,
385417
];
386418

387419
for (const fn of funcs) {

0 commit comments

Comments
 (0)