@@ -42,7 +42,6 @@ let debug = require('internal/util/debuglog').debuglog('net', (fn) => {
4242 debug = fn ;
4343} ) ;
4444const {
45- kWrapConnectedHandle,
4645 isIP,
4746 isIPv4,
4847 isIPv6,
@@ -53,7 +52,8 @@ const assert = require('internal/assert');
5352const {
5453 UV_EADDRINUSE ,
5554 UV_EINVAL ,
56- UV_ENOTCONN
55+ UV_ENOTCONN ,
56+ UV_ECANCELED
5757} = internalBinding ( 'uv' ) ;
5858
5959const { Buffer } = require ( 'buffer' ) ;
@@ -1064,43 +1064,54 @@ function internalConnect(
10641064}
10651065
10661066
1067- function internalConnectMultiple ( context ) {
1067+ function internalConnectMultiple ( context , canceled ) {
10681068 clearTimeout ( context [ kTimeout ] ) ;
10691069 const self = context . socket ;
1070- assert ( self . connecting ) ;
10711070
10721071 // 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 ) {
10741073 self . destroy ( aggregateErrors ( context . errors ) ) ;
10751074 return ;
10761075 }
10771076
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+
10781088 const { localPort, port, flags } = context ;
10791089 const { address, family : addressType } = context . addresses [ context . current ++ ] ;
1080- const handle = new TCP ( TCPConstants . SOCKET ) ;
10811090 let localAddress ;
10821091 let err ;
10831092
10841093 if ( localPort ) {
10851094 if ( addressType === 4 ) {
10861095 localAddress = DEFAULT_IPV4_ADDR ;
1087- err = handle . bind ( localAddress , localPort ) ;
1096+ err = self . _handle . bind ( localAddress , localPort ) ;
10881097 } else { // addressType === 6
10891098 localAddress = DEFAULT_IPV6_ADDR ;
1090- err = handle . bind6 ( localAddress , localPort , flags ) ;
1099+ err = self . _handle . bind6 ( localAddress , localPort , flags ) ;
10911100 }
10921101
10931102 debug ( 'connect/multiple: binding to localAddress: %s and localPort: %d (addressType: %d)' ,
10941103 localAddress , localPort , addressType ) ;
10951104
1096- err = checkBindError ( err , localPort , handle ) ;
1105+ err = checkBindError ( err , localPort , self . _handle ) ;
10971106 if ( err ) {
10981107 ArrayPrototypePush ( context . errors , exceptionWithHostPort ( err , 'bind' , localAddress , localPort ) ) ;
10991108 internalConnectMultiple ( context ) ;
11001109 return ;
11011110 }
11021111 }
11031112
1113+ debug ( 'connect/multiple: attempting to connect to %s:%d (addressType: %d)' , address , port , addressType ) ;
1114+
11041115 const req = new TCPConnectWrap ( ) ;
11051116 req . oncomplete = FunctionPrototypeBind ( afterConnectMultiple , undefined , context ) ;
11061117 req . address = address ;
@@ -1111,9 +1122,9 @@ function internalConnectMultiple(context) {
11111122 ArrayPrototypePush ( self . autoSelectFamilyAttemptedAddresses , `${ address } :${ port } ` ) ;
11121123
11131124 if ( addressType === 4 ) {
1114- err = handle . connect ( req , address , port ) ;
1125+ err = self . _handle . connect ( req , address , port ) ;
11151126 } else {
1116- err = handle . connect6 ( req , address , port ) ;
1127+ err = self . _handle . connect6 ( req , address , port ) ;
11171128 }
11181129
11191130 if ( err ) {
@@ -1337,6 +1348,8 @@ function lookupAndConnectMultiple(self, async_id_symbol, lookup, host, options,
13371348 if ( ! self . connecting ) {
13381349 return ;
13391350 } else if ( err ) {
1351+ self . emit ( 'lookup' , err , undefined , undefined , host ) ;
1352+
13401353 // net.createConnection() creates a net.Socket object and immediately
13411354 // calls net.Socket.connect() on it (that's us). There are no event
13421355 // listeners registered yet so defer the error event to the next tick.
@@ -1529,7 +1542,7 @@ function afterConnectMultiple(context, status, handle, req, readable, writable)
15291542 ArrayPrototypePush ( context . errors , ex ) ;
15301543
15311544 // Try the next address
1532- internalConnectMultiple ( context ) ;
1545+ internalConnectMultiple ( context , status === UV_ECANCELED ) ;
15331546 return ;
15341547 }
15351548
@@ -1540,15 +1553,6 @@ function afterConnectMultiple(context, status, handle, req, readable, writable)
15401553 return ;
15411554 }
15421555
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-
15521556 if ( hasObserver ( 'net' ) ) {
15531557 startPerf (
15541558 self ,
0 commit comments