net: close socket when reset fails

uv_tcp_close_reset() fails with EINVAL while a shutdown is pending, as
it is after socket.end(). TCPWrap::Reset() marked the handle as closing
anyway, so it could never be closed. The socket did not emit 'close'
and the process could not exit.

Leave the handle state alone when the reset fails, and close the
handle normally in that case.

Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67800
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Guilherme Araújo authored and GitHub committed 2026-11-28 18:40:22 +00:00
1 parent 7dc685b002
commit 40c40fab36
3 files changed
+36 -2

No files matched your search

+4 -1
View File
@@ -1198,8 +1198,11 @@ Socket.prototype._destroy = function(exception, cb) {
debug('emit close');
this.emit('close', isException);
});
if (err)
if (err) {
// The handle is still open: close it normally.
closeSocketHandle(this, isException);
this.emit('error', new ErrnoException(err, 'reset'));
}
} else if (this._closeAfterHandlingError) {
// Enqueue closing the socket as a microtask, so that the socket can be
// accessible when an `error` event is handled in the `next tick queue`.
+4 -1
View File
@@ -624,8 +624,11 @@ int TCPWrap::Reset(Local<Value> close_callback) {
if (state_ != kInitialized) return 0;
int err = uv_tcp_close_reset(&handle_, OnClose);
// The handle is not closing on failure.
if (err != 0) return err;
state_ = kClosing;
if (!err & !close_callback.IsEmpty() && close_callback->IsFunction() &&
if (!close_callback.IsEmpty() && close_callback->IsFunction() &&
!persistent().IsEmpty()) {
object()
->Set(env()->context(), env()->handle_onclose_symbol(), close_callback)
@@ -0,0 +1,28 @@
'use strict';
// resetAndDestroy() right after end() fails with EINVAL. The socket must
// still close.
const common = require('../common');
const assert = require('assert');
const net = require('net');
const server = net.createServer(common.mustCall((socket) => {
socket.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'EINVAL');
assert.strictEqual(err.syscall, 'reset');
}));
socket.on('close', common.mustCall(() => {
assert.strictEqual(socket.destroyed, true);
}));
socket.end('bye');
socket.resetAndDestroy();
server.close();
}));
server.listen(0, common.mustCall(() => {
const client = net.connect(server.address().port);
client.resume();
client.on('close', common.mustCall());
}));