Skip to content

Commit b54452d

Browse files
pietermeesMylesBorins
authored andcommitted
http2: emit session connect on next tick
Backport-PR-URL: #20456 PR-URL: #19842 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
1 parent 29643cc commit b54452d

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
@@ -734,7 +734,7 @@ function setupHandle(socket, type, options) {
734734
// core will check for session.destroyed before progressing, this
735735
// ensures that those at l`east get cleared out.
736736
if (this.destroyed) {
737-
this.emit('connect', this, socket);
737+
process.nextTick(emit, this, 'connect', this, socket);
738738
return;
739739
}
740740
debug(`Http2Session ${sessionName(type)}: setting up session handle`);
@@ -776,7 +776,7 @@ function setupHandle(socket, type, options) {
776776
options.settings : {};
777777

778778
this.settings(settings);
779-
this.emit('connect', this, socket);
779+
process.nextTick(emit, this, 'connect', this, socket);
780780
}
781781

782782
// 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)