GeezerVison is showing github.com — tls: reduce TLS 'close' event listener warnings · nodejs/node@d2def15
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/_tls_wrap.js +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -713,7 +713,12 @@ TLSSocket.prototype._wrapHandle = function(wrap, handle, wrapHasActiveWriteFromP |
713 | 713 | this[kRes] = res; |
714 | 714 | defineHandleReading(this, handle); |
715 | 715 |
|
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 | + |
717 | 722 | if (wrap) { |
718 | 723 | wrap.on('close', () => this.destroy()); |
719 | 724 | } |
|
Collapse file
Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | + |
| 6 | +if (!common.hasCrypto) { |
| 7 | + common.skip('missing crypto'); |
| 8 | +} |
| 9 | + |
| 10 | +const events = require('events'); |
| 11 | +const fixtures = require('../common/fixtures'); |
| 12 | +const tls = require('tls'); |
| 13 | +const { kReinitializeHandle } = require('internal/net'); |
| 14 | + |
| 15 | +// Test that repeated calls to kReinitializeHandle() do not result in repeatedly |
| 16 | +// adding new listeners on the socket (i.e. no MaxListenersExceededWarnings) |
| 17 | + |
| 18 | +process.on('warning', common.mustNotCall()); |
| 19 | + |
| 20 | +const server = tls.createServer({ |
| 21 | + key: fixtures.readKey('agent1-key.pem'), |
| 22 | + cert: fixtures.readKey('agent1-cert.pem') |
| 23 | +}); |
| 24 | + |
| 25 | +server.listen(0, common.mustCall(function() { |
| 26 | + const socket = tls.connect({ |
| 27 | + port: this.address().port, |
| 28 | + rejectUnauthorized: false |
| 29 | + }); |
| 30 | + |
| 31 | + socket.on('secureConnect', common.mustCall(function() { |
| 32 | + for (let i = 0; i < events.defaultMaxListeners + 1; i++) { |
| 33 | + socket[kReinitializeHandle](); |
| 34 | + } |
| 35 | + |
| 36 | + socket.destroy(); |
| 37 | + })); |
| 38 | + |
| 39 | + socket.on('close', function() { |
| 40 | + server.close(); |
| 41 | + }); |
| 42 | +})); |