Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d602c7a

Browse filesBrowse 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 <targos@protonmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 745e1e6 commit d602c7a
Copy full SHA for d602c7a

File tree

Expand file treeCollapse file tree

2 files changed

+29
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+29
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/http2/core.js‎

Copy file name to clipboardExpand all lines: lib/internal/http2/core.js
+1-1Lines changed: 1 addition & 1 deletion
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
}
Collapse file
+28Lines changed: 28 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.