Repository navigation
http: respect Keep-Alive timeout hint when agent has no explicit timeout - #66737
Closed
DivyanshuX9 wants to merge 1 commit into
Closed
DivyanshuX9 wants to merge 1 commit into
DivyanshuX9 wants to merge 1 commit into
Conversation
Collaborator
|
Review requested:
|
Contributor
Author
|
This fixes #66734: an http.Agent with keepAlive: true and no timeout ignored the server’s Keep-Alive: timeout=N hint. It’s a one-line change in keepSocketAlive plus a regression test. It narrows but doesn’t fully close the race in #52649. Could a collaborator start CI and take a look when you have time? |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #66737 +/- ##
=======================================
Coverage 90.49% 90.50%
=======================================
Files 797 797
Lines 277598 277598
Branches 53336 53339 +3
=======================================
+ Hits 251220 251228 +8
+ Misses 16790 16783 -7
+ Partials 9588 9587 -1
🚀 New features to boost your workflow:
|
DivyanshuX9
force-pushed
the
fix/http-agent-keep-alive-timeout
branch
from
October 15, 2026 22:29
ef5f42f to
4363a84
Compare
When http.Agent is created with keepAlive: true and no explicit timeout, agentTimeout defaults to 0. The condition serverHintTimeout < agentTimeout is never true when agentTimeout === 0, so socket.setTimeout(0) is called, disabling the idle timer and leaving stale sockets in freeSockets. Fix by also applying the server hint when agentTimeout === 0. Fixes: nodejs#66734 Refs: nodejs#52649 Signed-off-by: Divyanshu Sharma <divyanshu88999@gmail.com>
DivyanshuX9
force-pushed
the
fix/http-agent-keep-alive-timeout
branch
from
October 16, 2026 03:33
4363a84 to
a12dfcb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When an http.Agent is created with keepAlive: true but no explicit timeout option, the server's Keep-Alive: timeout=N response header is silently ignored. The socket is returned to the pool with socket.setTimeout(0), which disables the idle timer entirely. The server eventually closes the connection, but the socket remains in freeSockets. The next request that reuses it receives ECONNRESET.
Root cause
In keepSocketAlive(), agentTimeout is derived from the agent's timeout option:
When no timeout is configured, agentTimeout is 0. The branch that applies the server hint:
can never be true when agentTimeout === 0 (since serverHintTimeout is always positive after buffer subtraction). As a result, socket.setTimeout(0) is called, disabling the idle timer.
Fix
Also apply the server hint when agentTimeout === 0 (i.e., no client-side timeout is configured). The server's hint is the only available signal, so it should be respected rather than discarded.
Test
Four cases covering:
server hint only (no agent timeout) — socket timer set to hint
agent timeout shorter than hint — agent timeout wins
agent timeout longer than hint — hint wins
no hint, no agent timeout — timer stays disabled (socket.setTimeout(0))
Refs: #52649
Fixes: #66734