@@ -173,6 +173,7 @@ const {
173173const { kTimeout } = require ( 'internal/timers' ) ;
174174const { isArrayBufferView } = require ( 'internal/util/types' ) ;
175175const { format } = require ( 'internal/util/inspect' ) ;
176+ const { AsyncResource } = require ( 'async_hooks' ) ;
176177
177178const { FileHandle } = internalBinding ( 'fs' ) ;
178179const binding = internalBinding ( 'http2' ) ;
@@ -241,6 +242,7 @@ const kPendingRequestCalls = Symbol('kPendingRequestCalls');
241242const kProceed = Symbol ( 'proceed' ) ;
242243const kProtocol = Symbol ( 'protocol' ) ;
243244const kRemoteSettings = Symbol ( 'remote-settings' ) ;
245+ const kRequestAsyncResource = Symbol ( 'requestAsyncResource' ) ;
244246const kSelectPadding = Symbol ( 'select-padding' ) ;
245247const kSentHeaders = Symbol ( 'sent-headers' ) ;
246248const kSentTrailers = Symbol ( 'sent-trailers' ) ;
@@ -408,7 +410,11 @@ function onSessionHeaders(handle, id, cat, flags, headers, sensitiveHeaders) {
408410 originSet . delete ( stream [ kOrigin ] ) ;
409411 }
410412 debugStream ( id , type , "emitting stream '%s' event" , event ) ;
411- process . nextTick ( emit , stream , event , obj , flags , headers ) ;
413+ const reqAsync = stream [ kRequestAsyncResource ] ;
414+ if ( reqAsync )
415+ reqAsync . runInAsyncScope ( process . nextTick , null , emit , stream , event , obj , flags , headers ) ;
416+ else
417+ process . nextTick ( emit , stream , event , obj , flags , headers ) ;
412418 }
413419 if ( endOfStream ) {
414420 stream . push ( null ) ;
@@ -1089,7 +1095,11 @@ function setupHandle(socket, type, options) {
10891095 ReflectApply ( this . origin , this , options . origins ) ;
10901096 }
10911097
1092- process . nextTick ( emit , this , 'connect' , this , socket ) ;
1098+ const reqAsync = this [ kRequestAsyncResource ] ;
1099+ if ( reqAsync )
1100+ reqAsync . runInAsyncScope ( process . nextTick , null , emit , this , 'connect' , this , socket ) ;
1101+ else
1102+ process . nextTick ( emit , this , 'connect' , this , socket ) ;
10931103}
10941104
10951105// Emits a close event followed by an error event if err is truthy. Used
@@ -1797,6 +1807,8 @@ class ClientHttp2Session extends Http2Session {
17971807 stream [ kSentHeaders ] = headers ;
17981808 stream [ kOrigin ] = `${ headers [ HTTP2_HEADER_SCHEME ] } ://` +
17991809 `${ getAuthority ( headers ) } ` ;
1810+ const asyncRes = new AsyncResource ( 'PendingRequest' ) ;
1811+ stream [ kRequestAsyncResource ] = asyncRes ;
18001812
18011813 // Close the writable side of the stream if options.endStream is set.
18021814 if ( options . endStream )
@@ -1819,7 +1831,7 @@ class ClientHttp2Session extends Http2Session {
18191831 }
18201832 }
18211833
1822- const onConnect = requestOnConnect . bind ( stream , headersList , options ) ;
1834+ const onConnect = asyncRes . bind ( requestOnConnect . bind ( stream , headersList , options ) ) ;
18231835 if ( this . connecting ) {
18241836 if ( this [ kPendingRequestCalls ] !== null ) {
18251837 this [ kPendingRequestCalls ] . push ( onConnect ) ;
0 commit comments