Skip to content

Commit 4715fe1

Browse files
committed
net: fix autoSelectFamily close behavior
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.
1 parent 9f3466b commit 4715fe1

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lib/net.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,11 @@ function internalConnectMultiple(context, canceled) {
11121112
let localAddress;
11131113
let err;
11141114

1115+
if (context.current > 1 && self[kReinitializeHandle]) {
1116+
self[kReinitializeHandle](handle);
1117+
handle = self._handle;
1118+
}
1119+
11151120
if (localPort) {
11161121
if (addressType === 4) {
11171122
localAddress = DEFAULT_IPV4_ADDR;
@@ -1621,11 +1626,6 @@ function afterConnectMultiple(context, current, status, handle, req, readable, w
16211626
return;
16221627
}
16231628

1624-
if (context.current > 1 && self[kReinitializeHandle]) {
1625-
self[kReinitializeHandle](handle);
1626-
handle = self._handle;
1627-
}
1628-
16291629
if (hasObserver('net')) {
16301630
startPerf(
16311631
self,

0 commit comments

Comments
 (0)