Skip to content

Commit 337b2df

Browse files
idetargos
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. 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 98d461e commit 337b2df

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
@@ -1426,7 +1426,7 @@ class ClientHttp2Session extends Http2Session {
14261426

14271427
const onConnect = requestOnConnect.bind(stream, headersList, options);
14281428
if (this.connecting) {
1429-
this.on('connect', onConnect);
1429+
this.once('connect', onConnect);
14301430
} else {
14311431
onConnect();
14321432
}
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)