Skip to content

Commit

Permalink
net: deferred self.destroy calls in internalConnect(Multiple) to next…
Browse files Browse the repository at this point in the history
… tick

self.destroy calls in the internalConnect adn internalConnectMultiple
functions have a narrow case where they can throw before an error
handler has been established. This change defers them to the next
tick to allow time for the error handler to be set.

Fixes: nodejs#48771
  • Loading branch information
timothyjrogers committed Dec 4, 2023
1 parent c7fda39 commit 0d9f934
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ function internalConnect(
err = checkBindError(err, localPort, self._handle);
if (err) {
const ex = new ExceptionWithHostPort(err, 'bind', localAddress, localPort);
self.destroy(ex);
process.nextTick(internalConnectErrorNT, self, ex);
return;
}
}
Expand Down Expand Up @@ -1088,12 +1088,16 @@ function internalConnect(
}

const ex = new ExceptionWithHostPort(err, 'connect', address, port, details);
self.destroy(ex);
process.nextTick(internalConnectErrorNT, self, ex);
} else if ((addressType === 6 || addressType === 4) && hasObserver('net')) {
startPerf(self, kPerfHooksNetConnectContext, { type: 'net', name: 'connect', detail: { host: address, port } });
}
}

function internalConnectErrorNT(self, ex) {
self.destroy(ex);
}


function internalConnectMultiple(context, canceled) {
clearTimeout(context[kTimeout]);
Expand All @@ -1107,11 +1111,11 @@ function internalConnectMultiple(context, canceled) {
// All connections have been tried without success, destroy with error
if (canceled || context.current === context.addresses.length) {
if (context.errors.length === 0) {
self.destroy(new ERR_SOCKET_CONNECTION_TIMEOUT());
process.nextTick(internalConnectMultipleErrorNT, self, new ERR_SOCKET_CONNECTION_TIMEOUT());
return;
}

self.destroy(new NodeAggregateError(context.errors));
process.nextTick(internalConnectMultipleErrorNT, self, new NodeAggregateError(context.errors));
return;
}

Expand Down Expand Up @@ -1186,6 +1190,10 @@ function internalConnectMultiple(context, canceled) {
}
}

function internalConnectMultipleErrorNT(self, ex) {
self.destroy(ex);
}

Socket.prototype.connect = function(...args) {
let normalized;
// If passed an array, it's treated as an array of arguments that have
Expand Down

0 comments on commit 0d9f934

Please sign in to comment.