Skip to content

Commit

Permalink
net: fix autoSelectFamily close behavior
Browse files Browse the repository at this point in the history
If the first connection is successful, but only after the connect
timeout, and all subsequent connections result in an error, the socket
will not emit the 'close' event, causing issues in places like 'http's
'Agent' which rely on the 'close' event to free up sockets.

This commit moves the reassignment of '_handle' to happen as soon as a
new connection is attempted, so that if the socket is destroyed, the
'_handle' will represent the last connection attempt (even if it
resulted in an error) and closing the handle will then cause the 'close'
event to be emitted.

See npm/cli#6409 for more background.
  • Loading branch information
dharesign committed Jun 5, 2023
1 parent 9f3466b commit 4715fe1
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,11 @@ function internalConnectMultiple(context, canceled) {
let localAddress;
let err;

if (context.current > 1 && self[kReinitializeHandle]) {
self[kReinitializeHandle](handle);
handle = self._handle;
}

if (localPort) {
if (addressType === 4) {
localAddress = DEFAULT_IPV4_ADDR;
Expand Down Expand Up @@ -1621,11 +1626,6 @@ function afterConnectMultiple(context, current, status, handle, req, readable, w
return;
}

if (context.current > 1 && self[kReinitializeHandle]) {
self[kReinitializeHandle](handle);
handle = self._handle;
}

if (hasObserver('net')) {
startPerf(
self,
Expand Down

0 comments on commit 4715fe1

Please sign in to comment.