Skip to content

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

Merged
mcollina merged 4 commits into
mainfrom
fix/abort-queued-requests
Nov 19, 2026
Merged

mcollina merged 4 commits into
mainfrom
fix/abort-queued-requests

Conversation

@mcollina

@mcollina mcollina commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

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

🤖 Generated with Claude Code

https://claude.ai/code/session_01Sfj4nCVnJiJpsfbRdvDxpz

A request aborted while it waited for a connection, in the Pool queue or
pending on a Client that was connecting or busy, only failed once it
reached a socket. It is now taken out of the queue and fails right away
with the signal's reason. A request whose signal is already aborted is
not dispatched, for every API: #6117 did this for request() only.
stream(), pipeline(), connect() and upgrade() now also reject with the
signal's reason in that case, as request() does, instead of a generic
RequestAbortedError.

The API handlers pass a private hook to dispatch() only when a signal is
set, so requests without a signal do no extra work. A connection that is
already being opened is left to finish and serves the next request.

Fixes: #386

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sfj4nCVnJiJpsfbRdvDxpz
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@codecov-commenter

codecov-commenter commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.52066% with 6 lines in your changes missing coverage. Please review.
✅ 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 ⚠️
Additional details and impacted files
@@            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     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

mcollina and others added 2 commits November 18, 2026 09:14
…abort

The stale-while-revalidate background request copied the caller's
dispatch options, including the queued-abort hook. A caller that aborted
could cancel the revalidation while it waited for a connection, so the
cache was never refreshed. Drop the hook from the background request.

Also add tests for a queued abort through every interceptor and for an
abort during a stale-if-error revalidation.

Refs: #386

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sfj4nCVnJiJpsfbRdvDxpz
Signed-off-by: Matteo Collina <hello@matteocollina.com>
A retry or a redirect dispatches the same handler again after an earlier
attempt started. The queued-abort hook then left the handler's abort on
that attempt's controller, so an abort while the next attempt waited for
a connection only took effect once it started. With stream(), an abort
while a retry that resumes a partial body waited was lost, and the
request completed successfully.

The handler's abort now calls the earlier attempt's controller, which
does nothing once that attempt is over, and then takes the waiting
attempt out of its queue.

Refs: #386

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sfj4nCVnJiJpsfbRdvDxpz
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina requested review from ShogunPanda, jasnell, metcoder95 and ronag and removed request for jasnell November 18, 2026 08:49
When the retry interceptor resumes a partly received body, the consumer
already holds the response and its controller still targets the earlier
attempt. Aborting it, through request()'s signal or by destroying the
body, errored the body but left the resume request queued: it was sent
once a connection freed up and its response was discarded.

The retry handler now keeps the function that takes its waiting retry
out of the queue, and its controller's abort calls it. The hook is only
added once a retry happens, so requests that are never retried do no
extra work, with or without a signal.

Refs: #386

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sfj4nCVnJiJpsfbRdvDxpz
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina merged commit f16d0a5 into main Nov 19, 2026
38 checks passed
@mcollina
mcollina deleted the fix/abort-queued-requests branch November 19, 2026 14:46
@github-actions github-actions Bot mentioned this pull request Nov 19, 2026
mcollina added a commit that referenced this pull request Nov 20, 2026
…rname (#6233)

Since #6219, a request aborted while it waits for a connection leaves the
queue. When a Client serves several TLS server names and the aborted
request was the last one for the name it was connecting for, _resume()
switched client[kServerName] to the next queued request's name while the
connection was pending. Once connected, that connection, whose certificate
was verified for the previous name, was used for the next request.

A connection attempt now records the servername it verifies. If the
Client switched to another name by the time it connects, the socket is
destroyed and a new connection is opened for the current name. When
_resume() switches the name, it also cancels the pending attempt, so the
request does not wait for a connection it cannot use. A certificate name
error now only fails the requests for the name it was raised for.

Refs: #6219

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client will connect even if all requests are aborted

5 participants