From 6222f2f06b9a4ec63e5c03e5f2ba63847bf54b94 Mon Sep 17 00:00:00 2001 From: Avocado Date: Tue, 25 Aug 2026 14:40:54 +0900 Subject: [PATCH] net: reject keep-alive delays that cannot be applied The keep-alive delays are given in milliseconds but the underlying socket options are configured in whole seconds, so a positive value below 1000 ms rounds down to 0. That leaves the system default in place instead of applying the requested timing, and there is nothing to indicate that the value had no effect. uv_tcp_keepalive() already rejects a delay outside [1, 32767] seconds, so the value is treated as invalid one layer down. Throw ERR_OUT_OF_RANGE for a positive initialDelay or interval that cannot be applied as requested, covering both the truncation to zero and the upper bound the socket options can carry. A non-positive value keeps its documented meaning of leaving the current setting unchanged, and Infinity is accepted as "no timeout" by callers such as Agent. Three existing tests passed delays that were silently ignored; test-async-hooks-http-parser-destroy had never configured keep-alive at all despite asking for it. Refs: https://github.com/nodejs/node/issues/57712 Signed-off-by: Avocado --- doc/api/net.md | 18 ++++- lib/net.js | 23 ++++++ .../test-async-hooks-http-parser-destroy.js | 2 +- .../test-net-keepalive-delay-range.js | 79 +++++++++++++++++++ test/parallel/test-net-keepalive.js | 5 +- .../parallel/test-net-persistent-keepalive.js | 2 +- 6 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-net-keepalive-delay-range.js diff --git a/doc/api/net.md b/doc/api/net.md index ab569a7b9c6f..30f139e757e3 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -1669,7 +1669,11 @@ corresponding system default unchanged. `initialDelay` and `interval` are specified in milliseconds but the underlying socket options are configured in whole seconds; the values are -divided by `1000` and rounded down before being applied. +divided by `1000` and rounded down before being applied. Sub-second timings +cannot be expressed, so a positive value below `1000` throws +[`ERR_OUT_OF_RANGE`][] rather than leaving the corresponding system default in +place. The largest delay the socket options can carry is `32767` seconds, and a +value above that throws as well. Enabling the keep-alive functionality will set the following socket options: @@ -1688,6 +1692,12 @@ those platforms. added: - v26.4.0 - v24.19.0 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/65528 + description: A positive `initialDelay` or `interval` that cannot be applied + as requested now throws `ERR_OUT_OF_RANGE` instead of being + silently altered. --> * `options` {Object} @@ -1709,6 +1719,11 @@ socket.setKeepAlive({ enable: true, initialDelay: 1000, interval: 1000, count: 1