diff --git a/lib/net.js b/lib/net.js index 691f6bf57c7fd1..73d1343f3d8e09 100644 --- a/lib/net.js +++ b/lib/net.js @@ -35,6 +35,7 @@ const { ObjectDefineProperty, ObjectSetPrototypeOf, Symbol, + SymbolDispose, } = primordials; const EventEmitter = require('events'); @@ -1605,9 +1606,10 @@ function afterConnect(status, handle, req, readable, writable) { function addClientAbortSignalOption(self, options) { validateAbortSignal(options.signal, 'options.signal'); const { signal } = options; + let disposable; function onAbort() { - signal.removeEventListener('abort', onAbort); + disposable?.[SymbolDispose](); self._aborted = true; } @@ -1615,7 +1617,7 @@ function addClientAbortSignalOption(self, options) { process.nextTick(onAbort); } else { process.nextTick(() => { - signal.addEventListener('abort', onAbort); + disposable = EventEmitter.addAbortListener(signal, onAbort); }); } } @@ -1695,8 +1697,8 @@ function addServerAbortSignalOption(self, options) { if (signal.aborted) { process.nextTick(onAborted); } else { - signal.addEventListener('abort', onAborted); - self.once('close', () => signal.removeEventListener('abort', onAborted)); + const disposable = EventEmitter.addAbortListener(signal, onAborted); + self.once('close', disposable[SymbolDispose]); } }