@@ -135,7 +135,6 @@ let autoSelectFamilyDefault = getOptionValue('--enable-network-family-autoselect
135
135
136
136
const { clearTimeout, setTimeout } = require ( 'timers' ) ;
137
137
const { kTimeout } = require ( 'internal/timers' ) ;
138
- const kTimeoutTriggered = Symbol ( 'kTimeoutTriggered' ) ;
139
138
140
139
const DEFAULT_IPV4_ADDR = '0.0.0.0' ;
141
140
const DEFAULT_IPV6_ADDR = '::' ;
@@ -1092,9 +1091,11 @@ function internalConnectMultiple(context) {
1092
1091
return ;
1093
1092
}
1094
1093
1094
+
1095
+ const current = context . current ++ ;
1096
+ const handle = current === 0 ? self . _handle : new TCP ( TCPConstants . SOCKET ) ;
1095
1097
const { localPort, port, flags } = context ;
1096
- const { address, family : addressType } = context . addresses [ context . current ++ ] ;
1097
- const handle = new TCP ( TCPConstants . SOCKET ) ;
1098
+ const { address, family : addressType } = context . addresses [ current ] ;
1098
1099
let localAddress ;
1099
1100
let err ;
1100
1101
@@ -1119,7 +1120,7 @@ function internalConnectMultiple(context) {
1119
1120
}
1120
1121
1121
1122
const req = new TCPConnectWrap ( ) ;
1122
- req . oncomplete = FunctionPrototypeBind ( afterConnectMultiple , undefined , context ) ;
1123
+ req . oncomplete = FunctionPrototypeBind ( afterConnectMultiple , undefined , context , current ) ;
1123
1124
req . address = address ;
1124
1125
req . port = port ;
1125
1126
req . localAddress = localAddress ;
@@ -1146,8 +1147,12 @@ function internalConnectMultiple(context) {
1146
1147
return ;
1147
1148
}
1148
1149
1149
- // If the attempt has not returned an error, start the connection timer
1150
- context [ kTimeout ] = setTimeout ( internalConnectMultipleTimeout , context . timeout , context , req ) ;
1150
+ if ( current < context . addresses . length - 1 ) {
1151
+ debug ( 'connect/multiple: setting the attempt timeout to %d ms' , context . timeout ) ;
1152
+
1153
+ // If the attempt has not returned an error, start the connection timer
1154
+ context [ kTimeout ] = setTimeout ( internalConnectMultipleTimeout , context . timeout , context , req , handle ) ;
1155
+ }
1151
1156
}
1152
1157
1153
1158
Socket . prototype . connect = function ( ...args ) {
@@ -1418,7 +1423,6 @@ function lookupAndConnectMultiple(self, async_id_symbol, lookup, host, options,
1418
1423
localPort,
1419
1424
timeout,
1420
1425
[ kTimeout ] : null ,
1421
- [ kTimeoutTriggered ] : false ,
1422
1426
errors : [ ] ,
1423
1427
} ;
1424
1428
@@ -1521,12 +1525,20 @@ function afterConnect(status, handle, req, readable, writable) {
1521
1525
}
1522
1526
}
1523
1527
1524
- function afterConnectMultiple ( context , status , handle , req , readable , writable ) {
1525
- const self = context . socket ;
1526
-
1528
+ function afterConnectMultiple ( context , current , status , handle , req , readable , writable ) {
1527
1529
// Make sure another connection is not spawned
1528
1530
clearTimeout ( context [ kTimeout ] ) ;
1529
1531
1532
+ // One of the connection has completed and correctly dispatched but after timeout, ignore this one
1533
+ if ( status === 0 && current !== context . current - 1 ) {
1534
+ debug ( 'connect/multiple: ignoring successful but timedout connection to %s:%s' , req . address , req . port ) ;
1535
+ handle . close ( ) ;
1536
+ return ;
1537
+ }
1538
+
1539
+ const self = context . socket ;
1540
+
1541
+
1530
1542
// Some error occurred, add to the list of exceptions
1531
1543
if ( status !== 0 ) {
1532
1544
let details ;
@@ -1551,7 +1563,7 @@ function afterConnectMultiple(context, status, handle, req, readable, writable)
1551
1563
}
1552
1564
1553
1565
// One of the connection has completed and correctly dispatched but after timeout, ignore this one
1554
- if ( context [ kTimeoutTriggered ] ) {
1566
+ if ( status === 0 && current !== context . current - 1 ) {
1555
1567
debug ( 'connect/multiple: ignoring successful but timedout connection to %s:%s' , req . address , req . port ) ;
1556
1568
handle . close ( ) ;
1557
1569
return ;
@@ -1577,8 +1589,10 @@ function afterConnectMultiple(context, status, handle, req, readable, writable)
1577
1589
afterConnect ( status , handle , req , readable , writable ) ;
1578
1590
}
1579
1591
1580
- function internalConnectMultipleTimeout ( context , req ) {
1581
- context [ kTimeoutTriggered ] = true ;
1592
+ function internalConnectMultipleTimeout ( context , req , handle ) {
1593
+ debug ( 'connect/multiple: connection to %s:%s timed out' , req . address , req . port ) ;
1594
+ req . oncomplete = undefined ;
1595
+ handle . close ( ) ;
1582
1596
internalConnectMultiple ( context ) ;
1583
1597
}
1584
1598
0 commit comments