Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: Prevent unnecessary listeners from being registered #27987

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const kLocalSettings = Symbol('local-settings');
const kOptions = Symbol('options');
const kOwner = owner_symbol;
const kOrigin = Symbol('origin');
const kPendingRequestCalls = Symbol('kPendingRequestCalls');
const kProceed = Symbol('proceed');
const kProtocol = Symbol('protocol');
const kRemoteSettings = Symbol('remote-settings');
Expand Down Expand Up @@ -1410,6 +1411,7 @@ class ServerHttp2Session extends Http2Session {
class ClientHttp2Session extends Http2Session {
constructor(options, socket) {
super(NGHTTP2_SESSION_CLIENT, options, socket);
this[kPendingRequestCalls] = null;
}

// Submits a new HTTP2 request to the connected peer. Returns the
Expand Down Expand Up @@ -1479,7 +1481,15 @@ class ClientHttp2Session extends Http2Session {

const onConnect = requestOnConnect.bind(stream, headersList, options);
if (this.connecting) {
this.once('connect', onConnect);
if (this[kPendingRequestCalls] !== null) {
this[kPendingRequestCalls].push(onConnect);
} else {
this[kPendingRequestCalls] = [onConnect];
this.once('connect', () => {
this[kPendingRequestCalls].forEach((f) => f());
this[kPendingRequestCalls] = null;
});
}
} else {
onConnect();
}
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-http2-client-request-listeners-warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const http2 = require('http2');
const EventEmitter = require('events');

// This test ensures that a MaxListenersExceededWarning isn't emitted if
// more than EventEmitter.defaultMaxListeners requests are started on a
// ClientHttp2Session before it has finished connecting.

process.on('warning', common.mustNotCall('A warning was emitted'));

const server = http2.createServer();
server.on('stream', (stream) => {
stream.respond();
stream.end();
});

server.listen(common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);

function request() {
return new Promise((resolve, reject) => {
const stream = client.request();
stream.on('error', reject);
stream.on('response', resolve);
stream.end();
});
}

const requests = [];
for (let i = 0; i < EventEmitter.defaultMaxListeners + 1; i++) {
requests.push(request());
}

Promise.all(requests).then(common.mustCall()).finally(common.mustCall(() => {
server.close();
client.close();
}));
}));