Skip to content

http: respect Keep-Alive timeout hint when agent has no explicit timeout - #66737

Closed
DivyanshuX9 wants to merge 1 commit into
nodejs:mainfrom
DivyanshuX9:fix/http-agent-keep-alive-timeout
Closed

DivyanshuX9 wants to merge 1 commit into
nodejs:mainfrom
DivyanshuX9:fix/http-agent-keep-alive-timeout

Conversation

@DivyanshuX9

@DivyanshuX9 DivyanshuX9 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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:

let agentTimeout = this.options.timeout || 0;

When no timeout is configured, agentTimeout is 0. The branch that applies the server hint:

} else if (serverHintTimeout < agentTimeout) {

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

Copilot AI balanced review requested due to automatic review settings October 14, 2026 18:30
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@nodejs-github-bot nodejs-github-bot added http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Oct 14, 2026
@DivyanshuX9

Copy link
Copy Markdown
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

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.50%. Comparing base (1c90519) to head (4363a84).

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     
Files with missing lines Coverage Δ
lib/internal/http/agent.js 97.06% <100.00%> (+0.41%) ⬆️

... and 28 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@DivyanshuX9 DivyanshuX9 reopened this Oct 15, 2026
@DivyanshuX9
DivyanshuX9 force-pushed the fix/http-agent-keep-alive-timeout branch from ef5f42f to 4363a84 Compare October 15, 2026 22:29
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
DivyanshuX9 force-pushed the fix/http-agent-keep-alive-timeout branch from 4363a84 to a12dfcb Compare October 16, 2026 03:33
@DivyanshuX9
DivyanshuX9 deleted the fix/http-agent-keep-alive-timeout branch October 16, 2026 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http: Agent with keepAlive and no timeout ignores the server's Keep-Alive timeout hint

3 participants