@@ -52,7 +52,7 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
5252 const sessionCache = new SessionCache ( maxCachedSessions == null ? 100 : maxCachedSessions )
5353 timeout = timeout == null ? 10e3 : timeout
5454 allowH2 = allowH2 != null ? allowH2 : false
55- return function connect ( { hostname, host, protocol, port, servername, localAddress, httpSocket } , callback ) {
55+ return function connect ( { hostname, host, protocol, port, servername, localAddress, httpSocket, signal } , callback ) {
5656 let socket
5757 if ( protocol === 'https:' ) {
5858 if ( ! tls ) {
@@ -72,6 +72,7 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
7272 ...options ,
7373 servername,
7474 session,
75+ signal,
7576 localAddress,
7677 ALPNProtocols : allowH2 ? [ 'http/1.1' , 'h2' ] : [ 'http/1.1' ] ,
7778 socket : httpSocket , // upgrade socket connection
@@ -92,6 +93,7 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
9293 socket = net . connect ( {
9394 highWaterMark : 64 * 1024 , // Same as nodejs fs streams.
9495 ...options ,
96+ signal,
9597 localAddress,
9698 port,
9799 host : hostname
@@ -106,16 +108,11 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
106108
107109 const clearConnectTimeout = util . setupConnectTimeout ( new WeakRef ( socket ) , { timeout, hostname, port } )
108110
109- // Use close event for proper cleanup instead of monkey-patching destroy
110- // The close event is the canonical cleanup event in Node.js and covers all scenarios
111- socket . once ( 'close' , ( ) => {
112- if ( clearConnectTimeout && typeof clearConnectTimeout === 'function' ) {
113- clearConnectTimeout ( )
114- }
115- } )
116-
117111 socket
118112 . setNoDelay ( true )
113+ . once ( 'close' , function ( ) {
114+ queueMicrotask ( clearConnectTimeout )
115+ } )
119116 . once ( protocol === 'https:' ? 'secureConnect' : 'connect' , function ( ) {
120117 queueMicrotask ( clearConnectTimeout )
121118
0 commit comments