diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 9beb8833a297fb..e496c3a87fd3ee 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -1384,7 +1384,7 @@ class ClientHttp2Session extends Http2Session { const onConnect = requestOnConnect.bind(stream, headersList, options); if (this.connecting) { - this.on('connect', onConnect); + this.once('connect', onConnect); } else { onConnect(); } diff --git a/test/parallel/test-http2-request-remove-connect-listener.js b/test/parallel/test-http2-request-remove-connect-listener.js new file mode 100644 index 00000000000000..61de140c225144 --- /dev/null +++ b/test/parallel/test-http2-request-remove-connect-listener.js @@ -0,0 +1,28 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); +const assert = require('assert'); +const http2 = require('http2'); + + +const server = http2.createServer(); +server.on('stream', common.mustCall((stream) => { + stream.respond(); + stream.end(); +})); + +server.listen(0, common.mustCall(() => { + const client = http2.connect(`http://localhost:${server.address().port}`); + const req = client.request(); + client.once('connect', common.mustCall()); + + req.on('response', common.mustCall(() => { + assert.strictEqual(client.listenerCount('connect'), 0); + })); + req.on('close', common.mustCall(() => { + server.close(); + client.close(); + })); +}));