Skip to content

Commit

Permalink
net: use addAbortListener
Browse files Browse the repository at this point in the history
PR-URL: nodejs#48550
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
atlowChemi authored and Ceres6 committed Aug 14, 2023
1 parent 2f73350 commit 177ec8a
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 177ec8a

Please sign in to comment.