← Back to Pull requests

fix: destroy a connection attempt that no request waits for #6221

Closed
unknown closed this pull request without merging on Nov 19, 2026 14:46 UTC
View on GitHub
unknown unknown commented on Nov 18, 2026 15:52 UTC
Author

Stacked on #6219: it uses the abort path that #6219 adds to take a waiting request out of the Client queue. The base is fix/abort-queued-requests. I'll retarget to main once #6219 lands.

Problem

When every request waiting on a connecting Client is aborted, the connection attempt keeps going. It holds the Client, and with it a Pool connection slot, until it connects or hits connectTimeout (10 s by default), and forever with connectTimeout: 0. That is #386, "Client will connect even if all requests are aborted".

#6219 makes it slightly worse in one case. The aborted request now leaves the Client queue, so the connecting Client is no longer busy and the Pool hands it the next request. That request then fails with the timeout of an attempt it did not start. On main it waited in the Pool queue and got a fresh connection after the timeout.

Repro: a connections: 1 pool whose first connection hangs in the TLS handshake while later ones work. The only request is aborted at 100 ms, then a request without a signal is sent. "Pending" means not settled after 3 s.

| connectTimeout | main | #6219 | this PR |
|---|---|---|---|
| 0 | aborted: pending; next: pending | aborted: AbortError at once; next: pending | aborted: AbortError at once; next: 200 after 8 ms |
| 2000 | aborted: pending; next: 200 after 2.1 s | aborted: AbortError at once; next: UND_ERR_CONNECT_TIMEOUT after 2.4 s | aborted: AbortError at once; next: 200 after 3 ms |
| default | aborted: pending; next: pending | aborted: AbortError at once; next: pending | aborted: AbortError at once; next: 200 after 3 ms |

A request() to a host that never answers (10.255.255.1), aborted at 200 ms, now lets the process exit at 200 ms instead of 10.5 s.

Changes

  • The Client keeps the attempt's socket. While it connects, the Client holds on to the socket the connector returns. The built-in connector already returns it.

  • Cancelled when nobody waits for it. When the last request waiting on a connecting Client is aborted, the Client destroys that socket. The next request opens a new connection.

  • The Pool gets a chance first. The check runs in a microtask, after the 'drain' that the abort emits. A request queued in the Pool is handed to the Client in that drain and keeps the attempt alive. So does a request dispatched in the same tick as the abort.

  • No connectionError. Pool, RoundRobinPool and BalancedPool retire or penalize a client on 'connectionError', so the cancellation does not emit it. It is published on undici:client:connectError, with an InformationalError, so diagnostics subscribers that saw beforeConnect see the attempt end.

  • The internal kConnect is never cancelled.

  • Custom connectors. A custom connector has to return the socket for its attempts to be cancelled. The Client's wrapper already passes the return value through. A connector that returns nothing keeps today's behavior. Connector.md documents this, and the examples in Connector.md and Client.md now return connector(...).
  • Not covered

  • fetch() (#4405). fetch() does not pass the hook from #6219, so its aborted requests stay in the Client queue and the attempt is not cancelled. Same as main: the process still waits for the connect timeout. Wiring fetch() to the hook would be a follow-up.

  • ProxyAgent and Socks5ProxyAgent. Their connectors are async and return a promise, so their attempts are not cancelled.

  • client.destroy() while connecting. Unchanged: the socket is closed when the attempt calls back. #4412 covers it and could use the same handle.
  • Tests

    test/cancel-unused-connect.js, 10 tests:

  • Destroyed:

  • - h1 and h2 pools, with the default connectTimeout and with 0;
    - a custom connector that returns its socket.

    The hanging socket is closed, the next request succeeds on a new connection, and no connectionError is emitted.
  • Published: the cancellation shows up on undici:client:connectError.

  • Kept:

  • - a request queued in the Pool;
    - a second pipelined request;
    - a request dispatched right after the abort;
    - a connector that does not return its socket.

    The waiting request is served on the original connection, and the server sees one connection.

    Without the change, the five "destroyed" and "published" tests fail. If the check runs without waiting for the drain, the "queued in the Pool" and "dispatched right after the abort" tests fail.

    Suites: unit 1724/1727, node-test 245/250, interceptors 513/516, cache-interceptor 87/87, h2 and fetch all pass. No failures; the rest are skipped. Lint is clean.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01Sfj4nCVnJiJpsfbRdvDxpz

    Diff: 597 lines (+447 / -150) across 7 files Reviewer: ronag Review duration: 1373m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 597 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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.383
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #6221"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 50% (MEDIUM) · Higher = larger blast radius
    50%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 19.8% · Safe to merge
    19.8%
    Repository