← Back to Pull requests

fix: reject requests aborted while waiting for a connection #6219

Merged
mcollina merged 14 files (939 lines) on Nov 19, 2026 14:46 UTC
View on GitHub
mcollina mcollina commented on Nov 18, 2026 06:00 UTC
Author

This relates to...

Fixes #386

Rationale

A request aborted while it waited for a connection only failed once it reached a socket. With a busy Pool, or a Client stuck connecting (for example a TLS handshake that never completes), the caller kept waiting long after the abort. #6117 handled a signal that is already aborted when request() is called; this handles the rest.

Changes

  • Pool queue: an aborted request is taken out of the count right away (stats.queued drops) and fails with the signal's reason. FixedQueue can't remove from the middle, so the entry is marked dead and skipped when a connection frees up. close() resolves if it was the last queued request.

  • Client queue: a pending request (the client is connecting, or the request waits behind a running one) is removed and fails right away. The client then emits 'drain', so a Pool can use it again.

  • APIs: request, stream, pipeline, connect and upgrade pass a private kQueuedAbort hook to dispatch(), only when signal is set. While the request waits, the dispatcher calls the hook with a function that takes the request out of its queue; that function is the handler's abort until the request starts. Requests without a signal take the same path as before.

  • Already aborted signal: no API dispatches the request, so no connection is opened. stream, pipeline, connect and upgrade now reject with signal.reason, as request does, instead of a generic RequestAbortedError.

  • The handler gets an already aborted controller, so the retry interceptor treats this as a caller abort and does not retry.

  • A connection attempt already in progress is not cancelled: it finishes and serves the next request.

  • Retries and redirects: they dispatch the same handler again after an earlier attempt started. While the next attempt waits for a connection, the handler's abort calls the earlier attempt's controller (a no-op once that attempt is over), then takes the waiting attempt out of its queue. With stream(), an abort while a retry that resumes a partial body waited used to be lost, and the request completed successfully.

  • Resumed retries: when the retry interceptor resumes a partly received body, the consumer already holds the response. The retry handler now keeps the function that takes its waiting retry out of the queue, and its controller's abort calls it. Aborting request(), by signal or by destroying the body, no longer leaves the resume request queued to be sent later. This hook is only added once a retry happens.

  • Cache interceptor: the stale-while-revalidate background request no longer carries the caller's hook, so aborting the caller can't cancel the revalidation.

  • Two dump-interceptor tests relied on an aborted request still being sent. They now abort from the server handler, once the request is in flight, and a new test checks that an already aborted request is not sent.
  • Features

    N/A

    Bug Fixes

    A request aborted while queued in a Pool, or pending on a Client, fails immediately with the abort reason (#386).

    Breaking Changes and Deprecations

    None intended. Two visible differences:

  • a queued request now rejects when it is aborted instead of when a connection frees up;

  • with an already aborted signal, stream, pipeline, connect and upgrade reject with signal.reason (not UND_ERR_ABORTED, unless that is the reason) and never connect.
  • Interceptors

    A request queued behind a busy connection rejects at once with the abort reason through dump, decompress, responseError, retry, redirect, cache, deduplicate, dns, and all of them composed; test/issue-386.js covers each. Unchanged from main: with deduplicate, a request that joined an identical one in flight only sees its own abort once the shared request starts, and aborting the first caller also fails the requests that joined it.

    Benchmark

    Client CPU per request compared to main (median over rotated runs; server in a separate process, one fresh client process per run):

    | scenario | change |
    |---|---:|
    | no signal, connections free | −0.1% |
    | signal per request, connections free | +1.9% |
    | no signal, requests queued | +0.6% |
    | signal per request, requests queued | +1.1% |
    | one shared signal, requests queued | +2.3% |

    The no-signal results are within noise. I also tried having Client and Pool listen to the signal themselves: it cost about +4% without a signal and +11% to +19% with one, and it broke tests that count abort listeners.

    Not covered here: fetch does not pass its signal to dispatch(). Its promise already rejects on abort, but the underlying request stays queued until it starts. That can be a follow-up.

    Status

    <!-- KEY: S = Skipped, x = complete -->

  • I have read and agreed to the [Developer's Certificate of Origin][cert]

  • Tested

  • Benchmarked (optional)

  • Documented

  • Review ready

  • In review

  • Merge ready
  • [cert]: https://github.com/nodejs/undici/blob/main/CONTRIBUTING.md#developers-certificate-of-origin

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01Sfj4nCVnJiJpsfbRdvDxpz

    Diff: 939 lines (+892 / -47) across 14 files Reviewer: ronag Review duration: 1965m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.62) × (1 − Review Confidence (0.58)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.62 · 939 lines diff across 14 files
    0.62
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.21 · 1 substantive comment(s)
    0.21
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    58%
    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.400
    Shallow Review Depth shallow_review_depth
    0.278
    Real PR Conversation Timeline
    5 items
    codecov-commenter codecov-commenter commented on Nov 18, 2026 06:22 UTC
    Contributor

    Codecov Report


    :x: Patch coverage is 97.52066% with 6 lines in your changes missing coverage. Please review.
    :white_check_mark: Project coverage is 91.69%. Comparing base (011d947) to head (7ea2aaa).

    | Files with missing lines | Patch % | Lines |
    |---|---|---|
    | lib/dispatcher/client.js | 86.66% | 6 Missing :warning: |

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

    @@            Coverage Diff             @@
    ##             main    #6219      +/-   ##
    ==========================================
    + Coverage   91.67%   91.69%   +0.01%     
    ==========================================
      Files         113      113              
      Lines       44489    44698     +209     
    ==========================================
    + Hits        40786    40985     +199     
    - Misses       3703     3713      +10     
    

    </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 18, 2026 21:33 UTC
    Approved Reviewer

    Approved these changes.

    ShogunPanda ShogunPanda approved these changes on Nov 19, 2026 05:47 UTC
    Approved Reviewer

    Approved these changes.

    metcoder95 metcoder95 approved these changes on Nov 19, 2026 07:46 UTC
    Approved Reviewer

    Approved these changes.

    Reviewers
    Assignees
    mcollina mcollina
    Labels
    risk: low confidence: 58.2% 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: 62.1% (MEDIUM) · Higher = larger blast radius
    62.1%
    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: 58.2% · Lower = rushed or shallow review
    58.2%
    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: 25.9% · Safe to merge
    25.9%
    Repository