Skip to content

Commit 1455b1d

Browse files
pietermeesMylesBorins
authored andcommitted
http2: emit session connect on next tick
Backport-PR-URL: #20456 PR-URL: #19842 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
1 parent 165d214 commit 1455b1d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/internal/http2/core.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ function setupHandle(socket, type, options) {
731731
// core will check for session.destroyed before progressing, this
732732
// ensures that those at l`east get cleared out.
733733
if (this.destroyed) {
734-
this.emit('connect', this, socket);
734+
process.nextTick(emit, this, 'connect', this, socket);
735735
return;
736736
}
737737
debug(`Http2Session ${sessionName(type)}: setting up session handle`);
@@ -773,7 +773,7 @@ function setupHandle(socket, type, options) {
773773
options.settings : {};
774774

775775
this.settings(settings);
776-
this.emit('connect', this, socket);
776+
process.nextTick(emit, this, 'connect', this, socket);
777777
}
778778

779779
// Emits a close event followed by an error event if err is truthy. Used

test/parallel/test-http2-connect.js

+25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ if (!hasCrypto)
55
skip('missing crypto');
66
const { doesNotThrow, throws } = require('assert');
77
const { createServer, connect } = require('http2');
8+
const { connect: netConnect } = require('net');
9+
10+
// check for session connect callback and event
811
{
912
const server = createServer();
1013
server.listen(0, mustCall(() => {
@@ -30,6 +33,28 @@ const { createServer, connect } = require('http2');
3033
}));
3134
}
3235

36+
// check for session connect callback on already connected socket
37+
{
38+
const server = createServer();
39+
server.listen(0, mustCall(() => {
40+
const { port } = server.address();
41+
42+
const onSocketConnect = () => {
43+
const authority = `http://localhost:${port}`;
44+
const createConnection = mustCall(() => socket);
45+
const options = { createConnection };
46+
connect(authority, options, mustCall(onSessionConnect));
47+
};
48+
49+
const onSessionConnect = (session) => {
50+
session.close();
51+
server.close();
52+
};
53+
54+
const socket = netConnect(port, mustCall(onSocketConnect));
55+
}));
56+
}
57+
3358
// check for https as protocol
3459
{
3560
const authority = 'https://localhost';

0 commit comments

Comments
 (0)