Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-net-setkeepalive-error.js
Original file line number Diff line number Diff line change
@@ -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();
}));
}));
}
Loading