@@ -1125,7 +1125,7 @@ function internalConnect(
11251125 err = checkBindError ( err , localPort , self . _handle ) ;
11261126 if ( err ) {
11271127 const ex = new ExceptionWithHostPort ( err , 'bind' , localAddress , localPort ) ;
1128- process . nextTick ( emitErrorAndDestroy , self , ex ) ;
1128+ self . destroy ( ex ) ;
11291129 return ;
11301130 }
11311131 }
@@ -1135,7 +1135,7 @@ function internalConnect(
11351135
11361136 if ( addressType === 6 || addressType === 4 ) {
11371137 if ( self . blockList ?. check ( address , `ipv${ addressType } ` ) ) {
1138- process . nextTick ( emitErrorAndDestroy , self , new ERR_IP_BLOCKED ( address ) ) ;
1138+ self . destroy ( new ERR_IP_BLOCKED ( address ) ) ;
11391139 return ;
11401140 }
11411141 const req = new TCPConnectWrap ( ) ;
@@ -1167,20 +1167,12 @@ function internalConnect(
11671167 }
11681168
11691169 const ex = new ExceptionWithHostPort ( err , 'connect' , address , port , details ) ;
1170- process . nextTick ( emitErrorAndDestroy , self , ex ) ;
1170+ self . destroy ( ex ) ;
11711171 } else if ( ( addressType === 6 || addressType === 4 ) && hasObserver ( 'net' ) ) {
11721172 startPerf ( self , kPerfHooksNetConnectContext , { type : 'net' , name : 'connect' , detail : { host : address , port } } ) ;
11731173 }
11741174}
11751175
1176- // Helper function to defer socket destruction to the next tick.
1177- // This ensures that error handlers have a chance to be set up
1178- // before the error is emitted, particularly important when using
1179- // http.request with a custom lookup function.
1180- function emitErrorAndDestroy ( self , err ) {
1181- self . destroy ( err ) ;
1182- }
1183-
11841176
11851177function internalConnectMultiple ( context , canceled ) {
11861178 clearTimeout ( context [ kTimeout ] ) ;
@@ -1194,11 +1186,11 @@ function internalConnectMultiple(context, canceled) {
11941186 // All connections have been tried without success, destroy with error
11951187 if ( canceled || context . current === context . addresses . length ) {
11961188 if ( context . errors . length === 0 ) {
1197- process . nextTick ( emitErrorAndDestroy , self , new ERR_SOCKET_CONNECTION_TIMEOUT ( ) ) ;
1189+ self . destroy ( new ERR_SOCKET_CONNECTION_TIMEOUT ( ) ) ;
11981190 return ;
11991191 }
12001192
1201- process . nextTick ( emitErrorAndDestroy , self , new NodeAggregateError ( context . errors ) ) ;
1193+ self . destroy ( new NodeAggregateError ( context . errors ) ) ;
12021194 return ;
12031195 }
12041196
0 commit comments