@@ -134,7 +134,6 @@ let autoSelectFamilyAttemptTimeoutDefault = 250;
134134
135135const { clearTimeout, setTimeout } = require ( 'timers' ) ;
136136const { kTimeout } = require ( 'internal/timers' ) ;
137- const kTimeoutTriggered = Symbol ( 'kTimeoutTriggered' ) ;
138137
139138const DEFAULT_IPV4_ADDR = '0.0.0.0' ;
140139const DEFAULT_IPV6_ADDR = '::' ;
@@ -1106,9 +1105,10 @@ function internalConnectMultiple(context, canceled) {
11061105
11071106 assert ( self . connecting ) ;
11081107
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 ) ;
11101110 const { localPort, port, flags } = context ;
1111- const { address, family : addressType } = context . addresses [ context . current ++ ] ;
1111+ const { address, family : addressType } = context . addresses [ current ] ;
11121112 let localAddress ;
11131113 let err ;
11141114
@@ -1135,7 +1135,7 @@ function internalConnectMultiple(context, canceled) {
11351135 debug ( 'connect/multiple: attempting to connect to %s:%d (addressType: %d)' , address , port , addressType ) ;
11361136
11371137 const req = new TCPConnectWrap ( ) ;
1138- req . oncomplete = FunctionPrototypeBind ( afterConnectMultiple , undefined , context ) ;
1138+ req . oncomplete = FunctionPrototypeBind ( afterConnectMultiple , undefined , context , current ) ;
11391139 req . address = address ;
11401140 req . port = port ;
11411141 req . localAddress = localAddress ;
@@ -1162,8 +1162,12 @@ function internalConnectMultiple(context, canceled) {
11621162 return ;
11631163 }
11641164
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+ }
11671171}
11681172
11691173Socket . prototype . connect = function ( ...args ) {
@@ -1478,7 +1482,6 @@ function lookupAndConnectMultiple(
14781482 localPort,
14791483 timeout,
14801484 [ kTimeout ] : null ,
1481- [ kTimeoutTriggered ] : false ,
14821485 errors : [ ] ,
14831486 } ;
14841487
@@ -1581,18 +1584,19 @@ function afterConnect(status, handle, req, readable, writable) {
15811584 }
15821585}
15831586
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+
15851591 // 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 ) {
15871593 debug ( 'connect/multiple: ignoring successful but timedout connection to %s:%s' , req . address , req . port ) ;
15881594 handle . close ( ) ;
15891595 return ;
15901596 }
15911597
15921598 const self = context . socket ;
15931599
1594- // Make sure another connection is not spawned
1595- clearTimeout ( context [ kTimeout ] ) ;
15961600
15971601 // Some error occurred, add to the list of exceptions
15981602 if ( status !== 0 ) {
@@ -1633,8 +1637,10 @@ function afterConnectMultiple(context, status, handle, req, readable, writable)
16331637 afterConnect ( status , handle , req , readable , writable ) ;
16341638}
16351639
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 ( ) ;
16381644 internalConnectMultiple ( context ) ;
16391645}
16401646
0 commit comments