Repository navigation
fix: reject requests aborted while waiting for a connection - #6219
Merged
Merged
Conversation
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
…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
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>
ronag
approved these changes
Nov 18, 2026
ShogunPanda
approved these changes
Nov 19, 2026
metcoder95
approved these changes
Nov 19, 2026
Merged
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
stats.queueddrops) and fails with the signal's reason.FixedQueuecan'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.'drain', so a Pool can use it again.request,stream,pipeline,connectandupgradepass a privatekQueuedAborthook todispatch(), only whensignalis 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'sabortuntil the request starts. Requests without a signal take the same path as before.stream,pipeline,connectandupgradenow reject withsignal.reason, asrequestdoes, instead of a genericRequestAbortedError.stream(), an abort while a retry that resumes a partial body waited used to be lost, and the request completed successfully.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.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:
stream,pipeline,connectandupgradereject withsignal.reason(notUND_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.jscovers each. Unchanged frommain: withdeduplicate, 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):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:
fetchdoes not pass its signal todispatch(). 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