@@ -60,6 +60,8 @@ const {
6060 owner_symbol,
6161 } ,
6262} = require ( 'internal/async_hooks' ) ;
63+ const { AsyncResource } = require ( 'async_hooks' ) ;
64+
6365const {
6466 AbortError,
6567 aggregateTwoErrors,
@@ -241,6 +243,7 @@ const kPendingRequestCalls = Symbol('kPendingRequestCalls');
241243const kProceed = Symbol ( 'proceed' ) ;
242244const kProtocol = Symbol ( 'protocol' ) ;
243245const kRemoteSettings = Symbol ( 'remote-settings' ) ;
246+ const kRequestAsyncResource = Symbol ( 'requestAsyncResource' ) ;
244247const kSelectPadding = Symbol ( 'select-padding' ) ;
245248const kSentHeaders = Symbol ( 'sent-headers' ) ;
246249const kSentTrailers = Symbol ( 'sent-trailers' ) ;
@@ -408,7 +411,11 @@ function onSessionHeaders(handle, id, cat, flags, headers, sensitiveHeaders) {
408411 originSet . delete ( stream [ kOrigin ] ) ;
409412 }
410413 debugStream ( id , type , "emitting stream '%s' event" , event ) ;
411- process . nextTick ( emit , stream , event , obj , flags , headers ) ;
414+ const reqAsync = stream [ kRequestAsyncResource ] ;
415+ if ( reqAsync )
416+ reqAsync . runInAsyncScope ( process . nextTick , null , emit , stream , event , obj , flags , headers ) ;
417+ else
418+ process . nextTick ( emit , stream , event , obj , flags , headers ) ;
412419 }
413420 if ( endOfStream ) {
414421 stream . push ( null ) ;
@@ -1797,6 +1804,8 @@ class ClientHttp2Session extends Http2Session {
17971804 stream [ kSentHeaders ] = headers ;
17981805 stream [ kOrigin ] = `${ headers [ HTTP2_HEADER_SCHEME ] } ://` +
17991806 `${ getAuthority ( headers ) } ` ;
1807+ const reqAsync = new AsyncResource ( 'PendingRequest' ) ;
1808+ stream [ kRequestAsyncResource ] = reqAsync ;
18001809
18011810 // Close the writable side of the stream if options.endStream is set.
18021811 if ( options . endStream )
@@ -1819,7 +1828,7 @@ class ClientHttp2Session extends Http2Session {
18191828 }
18201829 }
18211830
1822- const onConnect = requestOnConnect . bind ( stream , headersList , options ) ;
1831+ const onConnect = reqAsync . bind ( requestOnConnect . bind ( stream , headersList , options ) ) ;
18231832 if ( this . connecting ) {
18241833 if ( this [ kPendingRequestCalls ] !== null ) {
18251834 this [ kPendingRequestCalls ] . push ( onConnect ) ;
0 commit comments