From dc596d0607f00187905f5fdc0a290d4869d38a8c Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Sun, 17 Jan 2021 01:10:31 +0800 Subject: [PATCH] events: remove error listener on signal abort Fixes: https://github.com/nodejs/node/issues/36949 PR-URL: https://github.com/nodejs/node/pull/36969 Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- lib/events.js | 2 +- test/parallel/test-events-once.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index 8beb501678f647..75baac156aaf86 100644 --- a/lib/events.js +++ b/lib/events.js @@ -736,7 +736,7 @@ async function once(emitter, name, options = {}) { } function abortListener() { eventTargetAgnosticRemoveListener(emitter, name, resolver); - eventTargetAgnosticRemoveListener(emitter, 'error', resolver); + eventTargetAgnosticRemoveListener(emitter, 'error', errorListener); reject(lazyDOMException('The operation was aborted', 'AbortError')); } if (signal != null) { diff --git a/test/parallel/test-events-once.js b/test/parallel/test-events-once.js index 7c37f576c29fd3..14b8ea5815a61a 100644 --- a/test/parallel/test-events-once.js +++ b/test/parallel/test-events-once.js @@ -169,6 +169,19 @@ async function abortSignalAfterEvent() { await once(ee, 'foo', { signal: ac.signal }); } +async function abortSignalRemoveListener() { + const ee = new EventEmitter(); + const ac = new AbortController(); + + try { + process.nextTick(() => ac.abort()); + await once(ee, 'test', { signal: ac.signal }); + } catch { + strictEqual(ee.listeners('test').length, 0); + strictEqual(ee.listeners('error').length, 0); + } +} + async function eventTargetAbortSignalBefore() { const et = new EventTarget(); const ac = new AbortController(); @@ -218,6 +231,7 @@ Promise.all([ abortSignalBefore(), abortSignalAfter(), abortSignalAfterEvent(), + abortSignalRemoveListener(), eventTargetAbortSignalBefore(), eventTargetAbortSignalAfter(), eventTargetAbortSignalAfterEvent(),