Skip to content

Commit 43c4a9e

Browse files
committed
tls: reduce TLS 'close' event listener warnings
Without this, some heavy usage of TLS sockets can result in MaxListenersExceededWarning firing, from the 'this.on('close', ...)' line here. These appear to come from reinitializeHandle, which calls _wrapHandle repeatedly on the same socket instance.
1 parent 78a1570 commit 43c4a9e

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/_tls_wrap.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,12 @@ TLSSocket.prototype._wrapHandle = function(wrap, handle, wrapHasActiveWriteFromP
713713
this[kRes] = res;
714714
defineHandleReading(this, handle);
715715

716-
this.on('close', onSocketCloseDestroySSL);
716+
// Guard against adding multiple listeners, as this method may be called
717+
// repeatedly on the same socket by reinitializeHandle
718+
if (this.listenerCount('close', onSocketCloseDestroySSL) === 0) {
719+
this.on('close', onSocketCloseDestroySSL);
720+
}
721+
717722
if (wrap) {
718723
wrap.on('close', () => this.destroy());
719724
}

0 commit comments

Comments
 (0)