GeezerVison is showing github.com — net,tls: pass a valid socket on `tlsClientError` · nodejs/node@6a4e99b
File treeExpand 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 +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -423,6 +423,10 @@ function onerror(err) { | 423 | 423 | if (!owner._secureEstablished) { | 424 | 424 | // When handshake fails control is not yet released, | 425 | 425 | // so self._tlsError will return null instead of actual error | | 426 | + | | 427 | + // Set closing the socket after emitting an event since the socket needs to | | 428 | + // be accessible when the `tlsClientError` event is emmited. | | 429 | + owner._closeAfterHandlingError = true; | 426 | 430 | owner.destroy(err); | 427 | 431 | } else if (owner._tlsOptions?.isServer && | 428 | 432 | owner._rejectUnauthorized && | |
Collapse file Copy file name to clipboardExpand all lines: lib/net.js +26-7Lines changed: 26 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -102,6 +102,7 @@ const { | 102 | 102 | uvExceptionWithHostPort, | 103 | 103 | } = require('internal/errors'); | 104 | 104 | const { isUint8Array } = require('internal/util/types'); | | 105 | +const { queueMicrotask } = require('internal/process/task_queues'); | 105 | 106 | const { | 106 | 107 | validateAbortSignal, | 107 | 108 | validateFunction, | @@ -284,6 +285,19 @@ function initSocketHandle(self) { | 284 | 285 | } | 285 | 286 | } | 286 | 287 |
| | 288 | +function closeSocketHandle(self, isException, isCleanupPending = false) { | | 289 | + if (self._handle) { | | 290 | + self._handle.close(() => { | | 291 | + debug('emit close'); | | 292 | + self.emit('close', isException); | | 293 | + if (isCleanupPending) { | | 294 | + self._handle.onread = noop; | | 295 | + self._handle = null; | | 296 | + self._sockname = null; | | 297 | + } | | 298 | + }); | | 299 | + } | | 300 | +} | 287 | 301 |
| 288 | 302 | const kBytesRead = Symbol('kBytesRead'); | 289 | 303 | const kBytesWritten = Symbol('kBytesWritten'); | @@ -332,6 +346,7 @@ function Socket(options) { | 332 | 346 | this[kBuffer] = null; | 333 | 347 | this[kBufferCb] = null; | 334 | 348 | this[kBufferGen] = null; | | 349 | + this._closeAfterHandlingError = false; | 335 | 350 |
| 336 | 351 | if (typeof options === 'number') | 337 | 352 | options = { fd: options }; // Legacy interface. | @@ -751,15 +766,19 @@ Socket.prototype._destroy = function(exception, cb) { | 751 | 766 | }); | 752 | 767 | if (err) | 753 | 768 | this.emit('error', errnoException(err, 'reset')); | | 769 | + } else if (this._closeAfterHandlingError) { | | 770 | + // Enqueue closing the socket as a microtask, so that the socket can be | | 771 | + // accessible when an `error` event is handled in the `next tick queue`. | | 772 | + queueMicrotask(() => closeSocketHandle(this, isException, true)); | 754 | 773 | } else { | 755 | | - this._handle.close(() => { | 756 | | - debug('emit close'); | 757 | | - this.emit('close', isException); | 758 | | - }); | | 774 | + closeSocketHandle(this, isException); | | 775 | + } | | 776 | + | | 777 | + if (!this._closeAfterHandlingError) { | | 778 | + this._handle.onread = noop; | | 779 | + this._handle = null; | | 780 | + this._sockname = null; | 759 | 781 | } | 760 | | - this._handle.onread = noop; | 761 | | - this._handle = null; | 762 | | - this._sockname = null; | 763 | 782 | cb(exception); | 764 | 783 | } else { | 765 | 784 | cb(exception); | |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common'); | | 3 | +const https = require('node:https'); | | 4 | +const assert = require('node:assert'); | | 5 | + | | 6 | +const server = https.createServer(); | | 7 | + | | 8 | +server.on( | | 9 | + 'tlsClientError', | | 10 | + common.mustCall((exception, tlsSocket) => { | | 11 | + assert.strictEqual(exception !== undefined, true); | | 12 | + assert.strictEqual(Object.keys(tlsSocket.address()).length !== 0, true); | | 13 | + assert.strictEqual(tlsSocket.localAddress !== undefined, true); | | 14 | + assert.strictEqual(tlsSocket.localPort !== undefined, true); | | 15 | + assert.strictEqual(tlsSocket.remoteAddress !== undefined, true); | | 16 | + assert.strictEqual(tlsSocket.remoteFamily !== undefined, true); | | 17 | + assert.strictEqual(tlsSocket.remotePort !== undefined, true); | | 18 | + }), | | 19 | +); | | 20 | + | | 21 | +server.listen(0, () => { | | 22 | + const req = https.request({ | | 23 | + hostname: '127.0.0.1', | | 24 | + port: server.address().port, | | 25 | + }); | | 26 | + req.on( | | 27 | + 'error', | | 28 | + common.mustCall(() => server.close()), | | 29 | + ); | | 30 | + req.end(); | | 31 | +}); |
|