diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 8005d7941a6df3..0aa9904001a340 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -774,7 +774,7 @@ function setupHandle(socket, type, options) { // core will check for session.destroyed before progressing, this // ensures that those at l`east get cleared out. if (this.destroyed) { - this.emit('connect', this, socket); + process.nextTick(emit, this, 'connect', this, socket); return; } debug(`Http2Session ${sessionName(type)}: setting up session handle`); @@ -816,7 +816,7 @@ function setupHandle(socket, type, options) { options.settings : {}; this.settings(settings); - this.emit('connect', this, socket); + process.nextTick(emit, this, 'connect', this, socket); } // Emits a close event followed by an error event if err is truthy. Used diff --git a/test/parallel/test-http2-connect.js b/test/parallel/test-http2-connect.js index 9b628db5aeebaa..252efa1229ad85 100644 --- a/test/parallel/test-http2-connect.js +++ b/test/parallel/test-http2-connect.js @@ -4,6 +4,9 @@ const { mustCall, hasCrypto, skip, expectsError } = require('../common'); if (!hasCrypto) skip('missing crypto'); const { createServer, connect } = require('http2'); +const { connect: netConnect } = require('net'); + +// check for session connect callback and event { const server = createServer(); server.listen(0, mustCall(() => { @@ -30,6 +33,28 @@ const { createServer, connect } = require('http2'); })); } +// check for session connect callback on already connected socket +{ + const server = createServer(); + server.listen(0, mustCall(() => { + const { port } = server.address(); + + const onSocketConnect = () => { + const authority = `http://localhost:${port}`; + const createConnection = mustCall(() => socket); + const options = { createConnection }; + connect(authority, options, mustCall(onSessionConnect)); + }; + + const onSessionConnect = (session) => { + session.close(); + server.close(); + }; + + const socket = netConnect(port, mustCall(onSocketConnect)); + })); +} + // check for https as protocol { const authority = 'https://localhost';