@@ -42,7 +42,6 @@ let debug = require('internal/util/debuglog').debuglog('net', (fn) => {
42
42
debug = fn ;
43
43
} ) ;
44
44
const {
45
- kWrapConnectedHandle,
46
45
isIP,
47
46
isIPv4,
48
47
isIPv6,
@@ -53,7 +52,8 @@ const assert = require('internal/assert');
53
52
const {
54
53
UV_EADDRINUSE ,
55
54
UV_EINVAL ,
56
- UV_ENOTCONN
55
+ UV_ENOTCONN ,
56
+ UV_ECANCELED
57
57
} = internalBinding ( 'uv' ) ;
58
58
59
59
const { Buffer } = require ( 'buffer' ) ;
@@ -1064,43 +1064,54 @@ function internalConnect(
1064
1064
}
1065
1065
1066
1066
1067
- function internalConnectMultiple ( context ) {
1067
+ function internalConnectMultiple ( context , canceled ) {
1068
1068
clearTimeout ( context [ kTimeout ] ) ;
1069
1069
const self = context . socket ;
1070
- assert ( self . connecting ) ;
1071
1070
1072
1071
// All connections have been tried without success, destroy with error
1073
- if ( context . current === context . addresses . length ) {
1072
+ if ( canceled || context . current === context . addresses . length ) {
1074
1073
self . destroy ( aggregateErrors ( context . errors ) ) ;
1075
1074
return ;
1076
1075
}
1077
1076
1077
+ assert ( self . connecting ) ;
1078
+
1079
+ // Reset the TCP handle when trying other addresses
1080
+ if ( context . current > 0 ) {
1081
+ if ( self ?. [ kHandle ] ?. _parent ) {
1082
+ self [ kHandle ] . _parent . reinitialize ( ) ;
1083
+ } else {
1084
+ self . _handle . reinitialize ( ) ;
1085
+ }
1086
+ }
1087
+
1078
1088
const { localPort, port, flags } = context ;
1079
1089
const { address, family : addressType } = context . addresses [ context . current ++ ] ;
1080
- const handle = new TCP ( TCPConstants . SOCKET ) ;
1081
1090
let localAddress ;
1082
1091
let err ;
1083
1092
1084
1093
if ( localPort ) {
1085
1094
if ( addressType === 4 ) {
1086
1095
localAddress = DEFAULT_IPV4_ADDR ;
1087
- err = handle . bind ( localAddress , localPort ) ;
1096
+ err = self . _handle . bind ( localAddress , localPort ) ;
1088
1097
} else { // addressType === 6
1089
1098
localAddress = DEFAULT_IPV6_ADDR ;
1090
- err = handle . bind6 ( localAddress , localPort , flags ) ;
1099
+ err = self . _handle . bind6 ( localAddress , localPort , flags ) ;
1091
1100
}
1092
1101
1093
1102
debug ( 'connect/multiple: binding to localAddress: %s and localPort: %d (addressType: %d)' ,
1094
1103
localAddress , localPort , addressType ) ;
1095
1104
1096
- err = checkBindError ( err , localPort , handle ) ;
1105
+ err = checkBindError ( err , localPort , self . _handle ) ;
1097
1106
if ( err ) {
1098
1107
ArrayPrototypePush ( context . errors , exceptionWithHostPort ( err , 'bind' , localAddress , localPort ) ) ;
1099
1108
internalConnectMultiple ( context ) ;
1100
1109
return ;
1101
1110
}
1102
1111
}
1103
1112
1113
+ debug ( 'connect/multiple: attempting to connect to %s:%d (addressType: %d)' , address , port , addressType ) ;
1114
+
1104
1115
const req = new TCPConnectWrap ( ) ;
1105
1116
req . oncomplete = FunctionPrototypeBind ( afterConnectMultiple , undefined , context ) ;
1106
1117
req . address = address ;
@@ -1111,9 +1122,9 @@ function internalConnectMultiple(context) {
1111
1122
ArrayPrototypePush ( self . autoSelectFamilyAttemptedAddresses , `${ address } :${ port } ` ) ;
1112
1123
1113
1124
if ( addressType === 4 ) {
1114
- err = handle . connect ( req , address , port ) ;
1125
+ err = self . _handle . connect ( req , address , port ) ;
1115
1126
} else {
1116
- err = handle . connect6 ( req , address , port ) ;
1127
+ err = self . _handle . connect6 ( req , address , port ) ;
1117
1128
}
1118
1129
1119
1130
if ( err ) {
@@ -1337,6 +1348,8 @@ function lookupAndConnectMultiple(self, async_id_symbol, lookup, host, options,
1337
1348
if ( ! self . connecting ) {
1338
1349
return ;
1339
1350
} else if ( err ) {
1351
+ self . emit ( 'lookup' , err , undefined , undefined , host ) ;
1352
+
1340
1353
// net.createConnection() creates a net.Socket object and immediately
1341
1354
// calls net.Socket.connect() on it (that's us). There are no event
1342
1355
// listeners registered yet so defer the error event to the next tick.
@@ -1529,7 +1542,7 @@ function afterConnectMultiple(context, status, handle, req, readable, writable)
1529
1542
ArrayPrototypePush ( context . errors , ex ) ;
1530
1543
1531
1544
// Try the next address
1532
- internalConnectMultiple ( context ) ;
1545
+ internalConnectMultiple ( context , status === UV_ECANCELED ) ;
1533
1546
return ;
1534
1547
}
1535
1548
@@ -1540,15 +1553,6 @@ function afterConnectMultiple(context, status, handle, req, readable, writable)
1540
1553
return ;
1541
1554
}
1542
1555
1543
- // Perform initialization sequence on the handle, then move on with the regular callback
1544
- self . _handle = handle ;
1545
- initSocketHandle ( self ) ;
1546
-
1547
- if ( self [ kWrapConnectedHandle ] ) {
1548
- self [ kWrapConnectedHandle ] ( handle ) ;
1549
- initSocketHandle ( self ) ; // This is called again to initialize the TLSWrap
1550
- }
1551
-
1552
1556
if ( hasObserver ( 'net' ) ) {
1553
1557
startPerf (
1554
1558
self ,
0 commit comments