Skip to content

Commit db307bd

Browse files
pietermeesjasnell
authored andcommitted
http2: emit session connect on next tick
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 645516c commit db307bd

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
@@ -774,7 +774,7 @@ function setupHandle(socket, type, options) {
774774
// core will check for session.destroyed before progressing, this
775775
// ensures that those at l`east get cleared out.
776776
if (this.destroyed) {
777-
this.emit('connect', this, socket);
777+
process.nextTick(emit, this, 'connect', this, socket);
778778
return;
779779
}
780780
debug(`Http2Session ${sessionName(type)}: setting up session handle`);
@@ -816,7 +816,7 @@ function setupHandle(socket, type, options) {
816816
options.settings : {};
817817

818818
this.settings(settings);
819-
this.emit('connect', this, socket);
819+
process.nextTick(emit, this, 'connect', this, socket);
820820
}
821821

822822
// 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
@@ -4,6 +4,9 @@ const { mustCall, hasCrypto, skip, expectsError } = require('../common');
44
if (!hasCrypto)
55
skip('missing crypto');
66
const { createServer, connect } = require('http2');
7+
const { connect: netConnect } = require('net');
8+
9+
// check for session connect callback and event
710
{
811
const server = createServer();
912
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)