Skip to content

Commit

Permalink
net: use addAbortListener
Browse files Browse the repository at this point in the history
PR-URL: #48550
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
atlowChemi authored and juanarbol committed Jul 13, 2023
1 parent 002ce31 commit ac11264
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
ObjectDefineProperty,
ObjectSetPrototypeOf,
Symbol,
SymbolDispose,
} = primordials;

const EventEmitter = require('events');
Expand Down Expand Up @@ -1605,17 +1606,18 @@ 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;
}

if (signal.aborted) {
process.nextTick(onAbort);
} else {
process.nextTick(() => {
signal.addEventListener('abort', onAbort);
disposable = EventEmitter.addAbortListener(signal, onAbort);
});
}
}
Expand Down Expand Up @@ -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]);
}
}

Expand Down

0 comments on commit ac11264

Please sign in to comment.