@@ -42,7 +42,7 @@ let debug = require('internal/util/debuglog').debuglog('net', (fn) => {
42
42
debug = fn ;
43
43
} ) ;
44
44
const {
45
- kWrapConnectedHandle ,
45
+ kReinitializeHandle ,
46
46
isIP,
47
47
isIPv4,
48
48
isIPv6,
@@ -53,7 +53,8 @@ const assert = require('internal/assert');
53
53
const {
54
54
UV_EADDRINUSE ,
55
55
UV_EINVAL ,
56
- UV_ENOTCONN
56
+ UV_ENOTCONN ,
57
+ UV_ECANCELED
57
58
} = internalBinding ( 'uv' ) ;
58
59
59
60
const { Buffer } = require ( 'buffer' ) ;
@@ -1064,17 +1065,18 @@ function internalConnect(
1064
1065
}
1065
1066
1066
1067
1067
- function internalConnectMultiple ( context ) {
1068
+ function internalConnectMultiple ( context , canceled ) {
1068
1069
clearTimeout ( context [ kTimeout ] ) ;
1069
1070
const self = context . socket ;
1070
- assert ( self . connecting ) ;
1071
1071
1072
1072
// All connections have been tried without success, destroy with error
1073
- if ( context . current === context . addresses . length ) {
1073
+ if ( canceled || context . current === context . addresses . length ) {
1074
1074
self . destroy ( aggregateErrors ( context . errors ) ) ;
1075
1075
return ;
1076
1076
}
1077
1077
1078
+ assert ( self . connecting ) ;
1079
+
1078
1080
const { localPort, port, flags } = context ;
1079
1081
const { address, family : addressType } = context . addresses [ context . current ++ ] ;
1080
1082
const handle = new TCP ( TCPConstants . SOCKET ) ;
@@ -1101,6 +1103,8 @@ function internalConnectMultiple(context) {
1101
1103
}
1102
1104
}
1103
1105
1106
+ debug ( 'connect/multiple: attempting to connect to %s:%d (addressType: %d)' , address , port , addressType ) ;
1107
+
1104
1108
const req = new TCPConnectWrap ( ) ;
1105
1109
req . oncomplete = FunctionPrototypeBind ( afterConnectMultiple , undefined , context ) ;
1106
1110
req . address = address ;
@@ -1190,6 +1194,13 @@ Socket.prototype.connect = function(...args) {
1190
1194
return this ;
1191
1195
} ;
1192
1196
1197
+ Socket . prototype [ kReinitializeHandle ] = function reinitializeHandle ( handle ) {
1198
+ this . _handle = handle ;
1199
+ this . _handle [ owner_symbol ] = this ;
1200
+
1201
+ initSocketHandle ( this ) ;
1202
+ }
1203
+
1193
1204
function socketToDnsFamily ( family ) {
1194
1205
switch ( family ) {
1195
1206
case 'IPv4' :
@@ -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,13 +1553,9 @@ 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
1556
+ if ( self [ kReinitializeHandle ] ) {
1557
+ self [ kReinitializeHandle ] ( handle ) ;
1558
+ handle = self . _handle ;
1550
1559
}
1551
1560
1552
1561
if ( hasObserver ( 'net' ) ) {
0 commit comments