Skip to content

Commit cd278b2

Browse files
committed
net,tls: pass a valid socket on tlsClientError
On the 'tlsClientError' event, the `tlsSocket` instance is passed as `closed` status. Thus, users can't get information such as `remote address`, `remoteFamily`, and so on. This adds a flag to close a socket after emitting an `error` event. Signed-off-by: Daeyeon Jeong [email protected]
1 parent 339c40d commit cd278b2

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

lib/_tls_wrap.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@ function onerror(err) {
423423
if (!owner._secureEstablished) {
424424
// When handshake fails control is not yet released,
425425
// 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;
426430
owner.destroy(err);
427431
} else if (owner._tlsOptions?.isServer &&
428432
owner._rejectUnauthorized &&

lib/net.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const {
102102
uvExceptionWithHostPort,
103103
} = require('internal/errors');
104104
const { isUint8Array } = require('internal/util/types');
105+
const { queueMicrotask } = require('internal/process/task_queues');
105106
const {
106107
validateAbortSignal,
107108
validateFunction,
@@ -280,6 +281,19 @@ function initSocketHandle(self) {
280281
}
281282
}
282283

284+
function closeSocketHandle(self, isException, isCleanupPending = false) {
285+
if (self._handle) {
286+
self._handle.close(() => {
287+
debug('emit close');
288+
self.emit('close', isException);
289+
if (isCleanupPending) {
290+
self._handle.onread = noop;
291+
self._handle = null;
292+
self._sockname = null;
293+
}
294+
});
295+
}
296+
}
283297

284298
const kBytesRead = Symbol('kBytesRead');
285299
const kBytesWritten = Symbol('kBytesWritten');
@@ -328,6 +342,7 @@ function Socket(options) {
328342
this[kBuffer] = null;
329343
this[kBufferCb] = null;
330344
this[kBufferGen] = null;
345+
this._closeAfterHandlingError = false;
331346

332347
if (typeof options === 'number')
333348
options = { fd: options }; // Legacy interface.
@@ -746,15 +761,19 @@ Socket.prototype._destroy = function(exception, cb) {
746761
});
747762
if (err)
748763
this.emit('error', errnoException(err, 'reset'));
764+
} else if (this._closeAfterHandlingError) {
765+
// Enqueue closing the socket as a microtask, so that the socket can be
766+
// accessible when an `error` event is handled in the `next tick queue`.
767+
queueMicrotask(() => closeSocketHandle(this, isException, true));
749768
} else {
750-
this._handle.close(() => {
751-
debug('emit close');
752-
this.emit('close', isException);
753-
});
769+
closeSocketHandle(this, isException);
770+
}
771+
772+
if (!this._closeAfterHandlingError) {
773+
this._handle.onread = noop;
774+
this._handle = null;
775+
this._sockname = null;
754776
}
755-
this._handle.onread = noop;
756-
this._handle = null;
757-
this._sockname = null;
758777
cb(exception);
759778
} else {
760779
cb(exception);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
});

0 commit comments

Comments
 (0)