← Back to Pull requests

fix(h1): do not reuse idle sockets whose keep-alive timeout expired #6171

Merged
mcollina merged 2 files (114 lines) on Nov 13, 2026 15:57 UTC
View on GitHub
mcollina mcollina commented on Nov 06, 2026 09:15 UTC
Author

This is the undici counterpart of https://github.com/nodejs/node/pull/67292.

Problem

The keep-alive timer of an idle HTTP/1.1 socket cannot fire while the event loop is blocked. Before reusing an idle socket, undici yields one setImmediate (scheduleIdleSocketValidation, added for GHSA-35p6-xmwp-9g52), so that a FIN or RST that has already been received is processed first. That only works if a poll phase runs between the dispatch and the check phase.

With a server in a separate process (keepAliveTimeout = 5000), one request, then a 6s synchronous block, then a second request:

| Second request dispatched from | Processed before the validation completes | Result on main |
|---|---|---|
| poll phase (I/O callback, server request handler, await after I/O) | nothing: the check phase comes next | ECONNRESET: the dead socket is reused |
| timers phase | the poll phase reads the FIN | reconnects |
| check phase (setImmediate) | the keep-alive timer fires | reconnects |

The poll-phase case is the common one, for example a proxy that does synchronous work in its request handler before calling upstream. A shorter version of the same problem: a block longer than the client's keep-alive timeout but shorter than the server's uses up the keepAliveTimeoutThreshold margin, and the request is sent just before the server closes the connection.

Fix

  • When resumeH1 arms the keep-alive timer, record performance.now() on the socket.

  • Before reusing an idle socket, destroy it with InformationalError('socket idle timeout') if its keep-alive timeout has passed. Nothing is running on the socket, so the pending requests are sent on a new connection, the same as when the timer fires.
  • The time is recorded when the timer is armed, so the check never expires a socket before the timer would have. It uses the global performance.now(), so it follows @sinonjs/fake-timers together with setTimeout. The cost is one performance.now() call when a socket becomes idle and one when it is reused.

    The check does not depend on kSocketUsed, so it also covers the experimental Milo parser. parser-h1.js never sets that symbol, which is private to client-h1.js. As a result, the existing idle socket validation does not run with Milo. That is a separate issue and is left for a follow-up.

    Not covered

    As in the Node.js change:

  • a server close that is still in flight when the request is sent (the general keep-alive race; the retry interceptor handles this for idempotent requests)

  • time spent blocked before the socket becomes idle
  • HTTP/2 is not changed.

    Testing

  • test/client-keep-alive-blocked-loop.js blocks inside an fs callback (poll phase) and checks that an expired socket is replaced and that a socket that has not expired is reused. The expiry case fails on main and passes with this change, with both llhttp and UNDICI_USE_MILO=1.

  • The out-of-process reproduction above passes for all three phases, and a 1s block still reuses the socket.

  • test/.js: 1664 passed, 0 failed, 3 skipped. test/node-test/*: 245 passed, 0 failed, 5 skipped.
  • 🤖 Generated with Claude Code

    https://claude.ai/code/session_01Sfj4nEWQ4ovycNDjbvXcRs

    Diff: 114 lines (+114 / -0) across 2 files Reviewer: ronag Review duration: 10482m
    All Vouch review confidence checks passed
    Residual risk 0.10 = Change Risk (0.33) × (1 − Review Confidence (0.71)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.33 · 114 lines diff across 2 files
    0.33
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.56 · 1 substantive comment(s)
    0.56
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    70%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.284
    Real PR Conversation Timeline
    3 items
    codecov-commenter codecov-commenter commented on Nov 06, 2026 09:38 UTC
    Contributor

    Codecov Report


    :white_check_mark: All modified and coverable lines are covered by tests.
    :white_check_mark: Project coverage is 91.68%. Comparing base (76f0b3e) to head (8f1768a).
    :warning: Report is 1 commits behind head on main.

    <details><summary>Additional details and impacted files</summary>

    @@           Coverage Diff           @@
    ##             main    #6171   +/-   ##
    =======================================
      Coverage   91.67%   91.68%           
    =======================================
      Files         113      113           
      Lines       44419    44467   +48     
    =======================================
    + Hits        40721    40768   +47     
    - Misses       3698     3699    +1     
    

    </details>

    :umbrella: View full report in Codecov by Harness.
    :loudspeaker: Have feedback on the report? Share it here.
    <details><summary> :rocket: New features to boost your workflow: </summary>

  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

  • </details>

    ronag ronag approved these changes on Nov 13, 2026 11:07 UTC
    Approved Reviewer

    Approved these changes.

    Reviewers
    Assignees
    mcollina mcollina
    Labels
    risk: low confidence: 70.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 32.8% (LOW) · Higher = larger blast radius
    32.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 70.9% · Lower = rushed or shallow review
    70.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 9.6% · Safe to merge
    9.6%
    Repository