Skip to content

Commit d2430ee

Browse files
orgadsruyadorno
authored andcommitted
http2: fix client async storage persistence
Create and store an AsyncResource for each stream, following a similar approach as used in HttpAgent. Fixes: #55376 PR-URL: #55460 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
1 parent 98bfc7d commit d2430ee

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎lib/internal/http2/core.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const {
6060
owner_symbol,
6161
},
6262
}=require('internal/async_hooks');
63+
const{ AsyncResource }=require('async_hooks');
64+
6365
const{
6466
AbortError,
6567
aggregateTwoErrors,
@@ -241,6 +243,7 @@ const kPendingRequestCalls = Symbol('kPendingRequestCalls');
241243
constkProceed=Symbol('proceed');
242244
constkProtocol=Symbol('protocol');
243245
constkRemoteSettings=Symbol('remote-settings');
246+
constkRequestAsyncResource=Symbol('requestAsyncResource');
244247
constkSelectPadding=Symbol('select-padding');
245248
constkSentHeaders=Symbol('sent-headers');
246249
constkSentTrailers=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+
constreqAsync=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);
@@ -1800,6 +1807,8 @@ class ClientHttp2Session extends Http2Session {
18001807
stream[kSentHeaders]=headers;
18011808
stream[kOrigin]=`${headers[HTTP2_HEADER_SCHEME]}://`+
18021809
`${getAuthority(headers)}`;
1810+
constreqAsync=newAsyncResource('PendingRequest');
1811+
stream[kRequestAsyncResource]=reqAsync;
18031812

18041813
// Close the writable side of the stream if options.endStream is set.
18051814
if(options.endStream)
@@ -1822,7 +1831,7 @@ class ClientHttp2Session extends Http2Session {
18221831
}
18231832
}
18241833

1825-
constonConnect=requestOnConnect.bind(stream,headersList,options);
1834+
constonConnect=reqAsync.bind(requestOnConnect.bind(stream,headersList,options));
18261835
if(this.connecting){
18271836
if(this[kPendingRequestCalls]!==null){
18281837
this[kPendingRequestCalls].push(onConnect);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto)
5+
common.skip('missing crypto');
6+
constassert=require('assert');
7+
consthttp2=require('http2');
8+
constasync_hooks=require('async_hooks');
9+
10+
conststorage=newasync_hooks.AsyncLocalStorage();
11+
12+
const{
13+
HTTP2_HEADER_CONTENT_TYPE,
14+
HTTP2_HEADER_PATH,
15+
HTTP2_HEADER_STATUS,
16+
}=http2.constants;
17+
18+
constserver=http2.createServer();
19+
server.on('stream',(stream)=>{
20+
stream.respond({
21+
[HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8',
22+
[HTTP2_HEADER_STATUS]: 200
23+
});
24+
stream.on('error',common.mustNotCall());
25+
stream.end('data');
26+
});
27+
28+
server.listen(0,async()=>{
29+
constclient=storage.run({id: 0},()=>http2.connect(`http://localhost:${server.address().port}`));
30+
31+
asyncfunctiondoReq(id){
32+
constreq=client.request({[HTTP2_HEADER_PATH]: '/'});
33+
34+
req.on('response',common.mustCall((headers)=>{
35+
assert.strictEqual(headers[HTTP2_HEADER_STATUS],200);
36+
assert.strictEqual(id,storage.getStore().id);
37+
}));
38+
req.on('data',common.mustCall((data)=>{
39+
assert.strictEqual(data.toString(),'data');
40+
assert.strictEqual(id,storage.getStore().id);
41+
}));
42+
req.on('end',common.mustCall(()=>{
43+
assert.strictEqual(id,storage.getStore().id);
44+
server.close();
45+
client.close();
46+
}));
47+
}
48+
49+
functiondoReqWith(id){
50+
storage.run({ id },()=>doReq(id));
51+
}
52+
53+
doReqWith(1);
54+
doReqWith(2);
55+
});

0 commit comments

Comments
 (0)