Skip to content

Commit d602c7a

Browse files
ideBethGriggs
authored andcommitted
http2: release request()'s "connect" event listener after it runs
The `Http2Session#request()` method internally listens to the "connect" event if the session has not yet established a connection so that the actual request can be sent after the connection has been established. This commit removes the event listener after it runs and carries out the request and is no longer needed. In practice this shouldn't affect the behavior of the session object since the "connect" event fires only once anyway, but removing the listener releases its references. The rest of this class subscribes to the "connect" event with `once` instead of `on` as well. Tested by adding a new test that ensures `Http2Session#request()` is called before the connection is established, indicated by a "connect" listener that is run. The test also ensures all "connect" listeners are removed after the connection is established. Backport-PR-URL: #22850 PR-URL: #21916 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 745e1e6 commit d602c7a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/internal/http2/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ class ClientHttp2Session extends Http2Session {
13841384

13851385
const onConnect = requestOnConnect.bind(stream, headersList, options);
13861386
if (this.connecting) {
1387-
this.on('connect', onConnect);
1387+
this.once('connect', onConnect);
13881388
} else {
13891389
onConnect();
13901390
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
9+
10+
const server = http2.createServer();
11+
server.on('stream', common.mustCall((stream) => {
12+
stream.respond();
13+
stream.end();
14+
}));
15+
16+
server.listen(0, common.mustCall(() => {
17+
const client = http2.connect(`http://localhost:${server.address().port}`);
18+
const req = client.request();
19+
client.once('connect', common.mustCall());
20+
21+
req.on('response', common.mustCall(() => {
22+
assert.strictEqual(client.listenerCount('connect'), 0);
23+
}));
24+
req.on('close', common.mustCall(() => {
25+
server.close();
26+
client.close();
27+
}));
28+
}));

0 commit comments

Comments
 (0)