@@ -134,7 +134,6 @@ let autoSelectFamilyAttemptTimeoutDefault = 250;
134
134
135
135
const { clearTimeout, setTimeout } = require ( 'timers' ) ;
136
136
const { kTimeout } = require ( 'internal/timers' ) ;
137
- const kTimeoutTriggered = Symbol ( 'kTimeoutTriggered' ) ;
138
137
139
138
const DEFAULT_IPV4_ADDR = '0.0.0.0' ;
140
139
const DEFAULT_IPV6_ADDR = '::' ;
@@ -1106,9 +1105,10 @@ function internalConnectMultiple(context, canceled) {
1106
1105
1107
1106
assert ( self . connecting ) ;
1108
1107
1109
- const handle = context . current === 0 ? self . _handle : new TCP ( TCPConstants . SOCKET ) ;
1108
+ const current = context . current ++ ;
1109
+ const handle = current === 0 ? self . _handle : new TCP ( TCPConstants . SOCKET ) ;
1110
1110
const { localPort, port, flags } = context ;
1111
- const { address, family : addressType } = context . addresses [ context . current ++ ] ;
1111
+ const { address, family : addressType } = context . addresses [ current ] ;
1112
1112
let localAddress ;
1113
1113
let err ;
1114
1114
@@ -1135,7 +1135,7 @@ function internalConnectMultiple(context, canceled) {
1135
1135
debug ( 'connect/multiple: attempting to connect to %s:%d (addressType: %d)' , address , port , addressType ) ;
1136
1136
1137
1137
const req = new TCPConnectWrap ( ) ;
1138
- req . oncomplete = FunctionPrototypeBind ( afterConnectMultiple , undefined , context ) ;
1138
+ req . oncomplete = FunctionPrototypeBind ( afterConnectMultiple , undefined , context , current ) ;
1139
1139
req . address = address ;
1140
1140
req . port = port ;
1141
1141
req . localAddress = localAddress ;
@@ -1162,8 +1162,12 @@ function internalConnectMultiple(context, canceled) {
1162
1162
return ;
1163
1163
}
1164
1164
1165
- // If the attempt has not returned an error, start the connection timer
1166
- context [ kTimeout ] = setTimeout ( internalConnectMultipleTimeout , context . timeout , context , req ) ;
1165
+ if ( current < context . addresses . length - 1 ) {
1166
+ debug ( 'connect/multiple: setting the attempt timeout to %d ms' , context . timeout ) ;
1167
+
1168
+ // If the attempt has not returned an error, start the connection timer
1169
+ context [ kTimeout ] = setTimeout ( internalConnectMultipleTimeout , context . timeout , context , req , handle ) ;
1170
+ }
1167
1171
}
1168
1172
1169
1173
Socket . prototype . connect = function ( ...args ) {
@@ -1478,7 +1482,6 @@ function lookupAndConnectMultiple(
1478
1482
localPort,
1479
1483
timeout,
1480
1484
[ kTimeout ] : null ,
1481
- [ kTimeoutTriggered ] : false ,
1482
1485
errors : [ ] ,
1483
1486
} ;
1484
1487
@@ -1581,18 +1584,19 @@ function afterConnect(status, handle, req, readable, writable) {
1581
1584
}
1582
1585
}
1583
1586
1584
- function afterConnectMultiple ( context , status , handle , req , readable , writable ) {
1587
+ function afterConnectMultiple ( context , current , status , handle , req , readable , writable ) {
1588
+ // Make sure another connection is not spawned
1589
+ clearTimeout ( context [ kTimeout ] ) ;
1590
+
1585
1591
// One of the connection has completed and correctly dispatched but after timeout, ignore this one
1586
- if ( context [ kTimeoutTriggered ] ) {
1592
+ if ( status === 0 && current !== context . current - 1 ) {
1587
1593
debug ( 'connect/multiple: ignoring successful but timedout connection to %s:%s' , req . address , req . port ) ;
1588
1594
handle . close ( ) ;
1589
1595
return ;
1590
1596
}
1591
1597
1592
1598
const self = context . socket ;
1593
1599
1594
- // Make sure another connection is not spawned
1595
- clearTimeout ( context [ kTimeout ] ) ;
1596
1600
1597
1601
// Some error occurred, add to the list of exceptions
1598
1602
if ( status !== 0 ) {
@@ -1633,8 +1637,10 @@ function afterConnectMultiple(context, status, handle, req, readable, writable)
1633
1637
afterConnect ( status , handle , req , readable , writable ) ;
1634
1638
}
1635
1639
1636
- function internalConnectMultipleTimeout ( context , req ) {
1637
- context [ kTimeoutTriggered ] = true ;
1640
+ function internalConnectMultipleTimeout ( context , req , handle ) {
1641
+ debug ( 'connect/multiple: connection to %s:%s timed out' , req . address , req . port ) ;
1642
+ req . oncomplete = undefined ;
1643
+ handle . close ( ) ;
1638
1644
internalConnectMultiple ( context ) ;
1639
1645
}
1640
1646
0 commit comments