Commit 3f614616313 for nodejs
commit 3f614616313ba23a1b5b72b4798c1ce9fe5c1730
Author: inoway46 <inoueyuya416@gmail.com>
Date: Wed Nov 4 22:26:51 2026 +0900
net: clear closed handles before emitting close
Clear a deferred socket handle before invoking close listeners. TLS has
already freed its SSL state at this point, so retaining the handle lets
getProtocol() access a null SSL pointer and crash the process.
Keep connection details available during tlsClientError and add coverage
for querying the protocol after a failed handshake closes the socket.
Signed-off-by: inoway46 <inoueyuya416@gmail.com>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/67215
Fixes: https://github.com/nodejs/node/issues/67213
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
diff --git a/lib/net.js b/lib/net.js
index ae7cca8ed6e..30a89119ace 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -368,12 +368,13 @@ function closeSocketHandle(self, isException, isCleanupPending = false) {
if (self._handle) {
self._handle.close(() => {
debug('emit close');
- self.emit('close', isException);
if (isCleanupPending) {
+ // The handle is closed and must not be used by close listeners.
self._handle.onread = noop;
self._handle = null;
self._sockname = null;
}
+ self.emit('close', isException);
});
}
}
diff --git a/test/parallel/test-tls-client-error-close.js b/test/parallel/test-tls-client-error-close.js
new file mode 100644
index 00000000000..d96ce760dec
--- /dev/null
+++ b/test/parallel/test-tls-client-error-close.js
@@ -0,0 +1,42 @@
+'use strict';
+
+const common = require('../common');
+if (!common.hasCrypto)
+ common.skip('missing crypto');
+
+const assert = require('assert');
+const net = require('net');
+const tls = require('tls');
+const fixtures = require('../common/fixtures');
+
+const server = tls.createServer({
+ key: fixtures.readKey('agent1-key.pem'),
+ cert: fixtures.readKey('agent1-cert.pem'),
+}, common.mustNotCall());
+
+server.on('tlsClientError', common.mustCall((err, socket) => {
+ assert.match(err.code, /^ERR_SSL_/);
+ assert(socket.destroyed);
+ // Connection details must remain available while handling the error.
+ assert.strictEqual(socket.remoteAddress, '127.0.0.1');
+ assert.strictEqual(socket.remoteFamily, 'IPv4');
+ assert(socket.remotePort > 0);
+ socket.on('close', common.mustCall(() => {
+ // The SSL state has been freed before close listeners run.
+ assert.strictEqual(socket.getProtocol(), null);
+ server.close(common.mustCall());
+ }));
+}));
+
+server.listen(0, '127.0.0.1', common.mustCall(() => {
+ const client = net.connect(server.address().port, '127.0.0.1');
+ client.on('connect', common.mustCall(() => {
+ // Reject the handshake without a TLS client.
+ client.write(Buffer.alloc(64, 42));
+ }));
+ client.on('error', common.mustCallAtLeast((err) => {
+ assert.strictEqual(err.code, 'ECONNRESET');
+ }, 0));
+ client.on('close', common.mustCall());
+ client.resume();
+}));