Skip to content

Commit 40ef2da

Browse files
authored
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 4f68c7c commit 40ef2da

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
@@ -1167,14 +1167,8 @@ function on(emitter, event, options = kEmptyObject) {
11671167
addEventListener(emitter, closeEvents[i], closeHandler);
11681168
}
11691169
}
1170-
if (signal) {
1171-
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
1172-
eventTargetAgnosticAddListener(
1173-
signal,
1174-
'abort',
1175-
abortListener,
1176-
{ __proto__: null, once: true, [kResistStopPropagation]: true });
1177-
}
1170+
1171+
const abortListenerDisposable = signal ? addAbortListener(signal, abortListener) : null;
11781172

11791173
return iterator;
11801174

@@ -1201,6 +1195,7 @@ function on(emitter, event, options = kEmptyObject) {
12011195
}
12021196

12031197
function closeHandler() {
1198+
abortListenerDisposable?.[SymbolDispose]();
12041199
removeAll();
12051200
finished = true;
12061201
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() {
@@ -372,6 +373,36 @@ async function abortableOnAfterDone() {
372373
});
373374
}
374375

376+
async function abortListenerRemovedAfterComplete() {
377+
const ee = new EventEmitter();
378+
const ac = new AbortController();
379+
380+
const i = setInterval(() => ee.emit('foo', 'foo'), 1);
381+
try {
382+
// Below: either the kEvents map is empty or the 'abort' listener list is empty
383+
384+
// Return case
385+
const endedIterator = on(ee, 'foo', { signal: ac.signal });
386+
assert.ok(ac.signal[kEvents].get('abort').size > 0);
387+
endedIterator.return();
388+
assert.strictEqual(ac.signal[kEvents].get('abort')?.size ?? ac.signal[kEvents].size, 0);
389+
390+
// Throw case
391+
const throwIterator = on(ee, 'foo', { signal: ac.signal });
392+
assert.ok(ac.signal[kEvents].get('abort').size > 0);
393+
throwIterator.throw(new Error());
394+
assert.strictEqual(ac.signal[kEvents].get('abort')?.size ?? ac.signal[kEvents].size, 0);
395+
396+
// Abort case
397+
on(ee, 'foo', { signal: ac.signal });
398+
assert.ok(ac.signal[kEvents].get('abort').size > 0);
399+
ac.abort(new Error());
400+
assert.strictEqual(ac.signal[kEvents].get('abort')?.size ?? ac.signal[kEvents].size, 0);
401+
} finally {
402+
clearInterval(i);
403+
}
404+
}
405+
375406
async function run() {
376407
const funcs = [
377408
basic,
@@ -391,6 +422,7 @@ async function run() {
391422
eventTargetAbortableOnAfter,
392423
eventTargetAbortableOnAfter2,
393424
abortableOnAfterDone,
425+
abortListenerRemovedAfterComplete,
394426
];
395427

396428
for (const fn of funcs) {

0 commit comments

Comments
 (0)