Skip to content

Commit c162101

Browse files
committed
http2: Fix client async storage persistence
Create and store an AsyncResource for each stream, following a similar approach as used in HttpAgent. Fixes: #55376
1 parent 99c6e4e commit c162101

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

lib/internal/http2/core.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ const {
173173
const { kTimeout } = require('internal/timers');
174174
const { isArrayBufferView } = require('internal/util/types');
175175
const { format } = require('internal/util/inspect');
176+
const { AsyncResource } = require('async_hooks');
176177

177178
const { FileHandle } = internalBinding('fs');
178179
const binding = internalBinding('http2');
@@ -241,6 +242,7 @@ const kPendingRequestCalls = Symbol('kPendingRequestCalls');
241242
const kProceed = Symbol('proceed');
242243
const kProtocol = Symbol('protocol');
243244
const kRemoteSettings = Symbol('remote-settings');
245+
const kRequestAsyncResource = Symbol('requestAsyncResource');
244246
const kSelectPadding = Symbol('select-padding');
245247
const kSentHeaders = Symbol('sent-headers');
246248
const 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);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
const assert = require('assert');
7+
const http2 = require('http2');
8+
const async_hooks = require('async_hooks');
9+
10+
const storage = new async_hooks.AsyncLocalStorage();
11+
12+
const {
13+
HTTP2_HEADER_CONTENT_TYPE,
14+
HTTP2_HEADER_PATH,
15+
HTTP2_HEADER_STATUS,
16+
} = http2.constants;
17+
18+
const server = 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+
const client = storage.run({ id: 0 }, () => http2.connect(`http://localhost:${server.address().port}`));
30+
31+
async function doReq(id) {
32+
const req = client.request({ [HTTP2_HEADER_PATH]: '/' });
33+
34+
req.on('response', common.mustCall((headers) => {
35+
assert.strictEqual(headers[HTTP2_HEADER_STATUS], 200);
36+
assert.equal(id, storage.getStore().id);
37+
}));
38+
req.on('data', common.mustCall((data) => {
39+
assert.equal(data, 'data');
40+
assert.equal(id, storage.getStore().id);
41+
}));
42+
req.on('end', common.mustCall(() => {
43+
assert.equal(id, storage.getStore().id);
44+
server.close();
45+
client.close();
46+
}));
47+
}
48+
49+
function doReqWith(id) {
50+
storage.run({ id }, () => doReq(id));
51+
}
52+
53+
doReqWith(1);
54+
doReqWith(2);
55+
});

0 commit comments

Comments
 (0)