From 302ff7217a0d45efbc54802a40ca49a2d80670a3 Mon Sep 17 00:00:00 2001 From: water <672684719@qq.com> Date: Tue, 25 Aug 2026 19:37:53 +0800 Subject: [PATCH 1/2] net: surface errors from setKeepAlive handle calls --- lib/net.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/net.js b/lib/net.js index d2b510c64bbb..0ffa03536b05 100644 --- a/lib/net.js +++ b/lib/net.js @@ -885,7 +885,10 @@ Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs, this[kSetKeepAliveInitialDelay] = initialDelay; this[kSetKeepAliveInterval] = interval; this[kSetKeepAliveCount] = count; - this._handle.setKeepAlive(enable, initialDelay, interval, count); + const err = this._handle.setKeepAlive(enable, initialDelay, interval, count); + if (err && !isWindows) { + throw new ErrnoException(err, 'setKeepAlive'); + } } return this; From 68ad950a01ddd0a1eea97aa752aba9c0ba2b07d3 Mon Sep 17 00:00:00 2001 From: water <672684719@qq.com> Date: Tue, 25 Aug 2026 19:39:26 +0800 Subject: [PATCH 2/2] test: add regression test for setKeepAlive handle errors --- test/parallel/test-net-setkeepalive-error.js | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/parallel/test-net-setkeepalive-error.js diff --git a/test/parallel/test-net-setkeepalive-error.js b/test/parallel/test-net-setkeepalive-error.js new file mode 100644 index 000000000000..6aa505c3491b --- /dev/null +++ b/test/parallel/test-net-setkeepalive-error.js @@ -0,0 +1,39 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +// Regression: setKeepAlive() silently ignored errors returned by the +// underlying handle (https://github.com/nodejs/node/issues/65529). +// It should throw like setTypeOfService() does, instead of reporting +// success when the operation failed. +if (common.isWindows) { + common.skip('keep-alive errors are treated as best-effort on Windows'); +} else { + const server = net.createServer(common.mustCall((socket) => { + socket.end(); + })); + + server.listen(0, common.mustCall(() => { + const client = net.connect(server.address().port, common.mustCall(() => { + // Make the handle report an error (EINVAL) for the keep-alive call. + const originalSetKeepAlive = client._handle.setKeepAlive; + client._handle.setKeepAlive = () => -22; // UV_EINVAL + try { + assert.throws( + () => client.setKeepAlive(true, 1000), + (err) => { + assert.strictEqual(err.code, 'EINVAL'); + assert.strictEqual(err.syscall, 'setKeepAlive'); + return true; + }, + ); + } finally { + client._handle.setKeepAlive = originalSetKeepAlive; + } + client.end(); + server.close(); + })); + })); +}