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

Reduce TLS 'close' event listener warnings caused by autoSelectFamily #50136

Merged
merged 2 commits into from
Oct 18, 2023
Merged
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
7 changes: 6 additions & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,12 @@ TLSSocket.prototype._wrapHandle = function(wrap, handle, wrapHasActiveWriteFromP
this[kRes] = res;
defineHandleReading(this, handle);

this.on('close', onSocketCloseDestroySSL);
// Guard against adding multiple listeners, as this method may be called
// repeatedly on the same socket by reinitializeHandle
if (this.listenerCount('close', onSocketCloseDestroySSL) === 0) {
this.on('close', onSocketCloseDestroySSL);
}

if (wrap) {
wrap.on('close', () => this.destroy());
}
Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-tls-reinitialize-listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');

if (!common.hasCrypto) {
common.skip('missing crypto');
}

const events = require('events');
const fixtures = require('../common/fixtures');
const tls = require('tls');
const { kReinitializeHandle } = require('internal/net');

// Test that repeated calls to kReinitializeHandle() do not result in repeatedly
// adding new listeners on the socket (i.e. no MaxListenersExceededWarnings)

process.on('warning', common.mustNotCall());

const server = tls.createServer({
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem')
});

server.listen(0, common.mustCall(function() {
const socket = tls.connect({
port: this.address().port,
rejectUnauthorized: false
});

socket.on('secureConnect', common.mustCall(function() {
for (let i = 0; i < events.defaultMaxListeners + 1; i++) {
socket[kReinitializeHandle]();
}

socket.destroy();
}));

socket.on('close', function() {
server.close();
});
}));