Skip to content

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

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

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

Conversation

@DivyanshuX9

Copy link
Copy Markdown
Contributor

Description

When an http.Agent is created with keepAlive: true but without an 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), disabling the idle timer entirely. The server eventually closes the idle connection, but the socket remains in freeSockets. Subsequent requests reusing the socket receive an 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 on the agent, agentTimeout is 0. The branch that applies the server hint:

} else if (serverHintTimeout < agentTimeout) {

can never evaluate to true when agentTimeout === 0 (since serverHintTimeout is positive after subtracting the buffer). As a result, socket.setTimeout(0) is invoked and the idle timer is disabled.

Fix

Apply the server hint when agentTimeout === 0 as well (agentTimeout === 0 || serverHintTimeout < agentTimeout). When no client-side timeout is specified, the server hint is the authoritative signal for connection lifetime and is applied to the socket timeout.

Tests

Added test/parallel/test-http-agent-keep-alive-server-hint.js covering four scenarios:

Server hint only (no agent timeout): Server hint applies to idle socket timer (hint - buffer).
Explicit agent timeout > server hint: Server hint wins.
Explicit agent timeout < server hint: Agent timeout wins.
No server hint, no agent timeout: Timer remains disabled (socket.setTimeout(0)), retaining original behavior.

Supersedes: #66737 Fixes: #66734
Refs: #52649

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>
Copilot AI balanced review requested due to automatic review settings October 16, 2026 03:45
@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 16, 2026
@codecov

codecov Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66779      +/-   ##
==========================================
+ Coverage   90.49%   90.51%   +0.01%     
==========================================
  Files         797      797              
  Lines      277598   277598              
  Branches    53336    53345       +9     
==========================================
+ Hits       251220   251257      +37     
+ Misses      16790    16776      -14     
+ Partials     9588     9565      -23     
Files with missing lines Coverage Δ
lib/internal/http/agent.js 97.06% <100.00%> (+0.41%) ⬆️

... and 29 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.

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