From c83098ad89c98d2b904c294fdd7ef7eed8b1a414 Mon Sep 17 00:00:00 2001 From: Pieter Mees Date: Mon, 9 Apr 2018 15:19:59 -0400 Subject: [PATCH] http2: emit session connect on next tick Backport-PR-URL: https://github.com/nodejs/node/pull/20456 PR-URL: https://github.com/nodejs/node/pull/19842 Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Anna Henningsen Reviewed-By: Anatoli Papirovski --- lib/internal/http2/core.js | 4 ++-- test/parallel/test-http2-connect.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index b5b1dc732ebde3..68bfbb043b0b63 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -734,7 +734,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`); @@ -776,7 +776,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 894c51fe3d9330..325a420b7e6e49 100644 --- a/test/parallel/test-http2-connect.js +++ b/test/parallel/test-http2-connect.js @@ -5,6 +5,9 @@ if (!hasCrypto) skip('missing crypto'); const { doesNotThrow, throws } = require('assert'); 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';