Skip to content

fix(fetch): take an aborted fetch out of the dispatcher queue - #6223

Open
mcollina wants to merge 2 commits into
mainfrom
fix/fetch-abort-queued
Open

mcollina wants to merge 2 commits into
mainfrom
fix/fetch-abort-queued

Conversation

@mcollina

@mcollina mcollina commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Builds on #6219 and #6221, both landed. Rebased on main.

Fixes #4405.

Problem

fetch() does not pass the queued-abort hook that #6219 adds. A fetch aborted while it waits for a connection stays in the Pool or Client queue until a connection is ready, so #6221 never sees that nobody waits for the connection attempt any more. The repro in #4405, a fetch to a host that never answers aborted after a second, rejects at once but keeps the process alive until the 10 s connect timeout. WebSocket.close() and EventSource.close() while connecting leave the attempt running the same way.

Scenario #6221 this PR
fetch() to a host that never answers, aborted at 200 ms rejects at 202 ms, process exits after 10.5 s rejects at 204 ms, process exits at 205 ms
fetch() aborted in the Pool queue stays queued until a connection frees leaves the queue at once
WebSocket.close() while the TLS handshake hangs close at once; the handshake keeps running close at once; the socket is closed
EventSource.close() while the TLS handshake hangs no event; the handshake keeps running no event; the socket is closed

Changes

  • fetch() passes the hook when it can be aborted. It does when its Request follows a signal. A Request without a signal has one that never aborts, so nothing can abort that fetch before it starts, and it gets no hook.
  • fetching() passes it by default. WebSocket and EventSource abort their fetch on close(), and Cache.addAll() aborts the other fetches when one fails.
  • Until the request starts, aborting or terminating the fetch takes it out of the queue. Its handler gets the abort reason, as for an abort after it starts.
  • Same chaining as for the other APIs. The hook chains with the abort of an attempt that already started, so an interceptor that dispatches again, such as retry, still reaches that attempt's controller.
  • EventSource no longer fires error after close(). That is an existing bug on main: closing while the request waits for response headers fires error with readyState already CLOSED. On main, a close while the TLS handshake hangs only reaches it after the connect timeout, and with this change it would happen at once, so it is fixed in its own commit. Per the spec, failing the connection fires error only "if the readyState attribute is set to a value other than CLOSED".

Cost

Client CPU per fetch(), median of 7 interleaved runs of 20,000 requests each, against #6221:

Scenario #6221 this PR Change
50 connections, 50 concurrent, no signal 46.02 µs 45.94 µs −0.2%
50 connections, 50 concurrent, signal 49.01 µs 49.18 µs +0.3%
10 connections, 100 concurrent (queued), no signal 47.33 µs 47.62 µs +0.6%
10 connections, 100 concurrent (queued), signal 49.43 µs 50.26 µs +1.7%

Only a fetch that has a signal and waits in a queue pays for the hook, about 0.8 µs. Passing the hook to every fetch, which I measured first, cost +2.2% on queued fetches without a signal.

Tests

test/fetch/issue-4405.js, 8 tests:

  • The hook: only a fetch that can be aborted passes it.
  • fetch() aborted while it waits:
    • in the Pool queue: it leaves the queue and is never sent;
    • while its TLS handshake hangs: the attempt is destroyed and the next fetch connects at once.
  • Retries:
  • WebSocket and EventSource closed while the TLS handshake hangs: the attempt is destroyed, and EventSource fires no error.

test/eventsource/eventsource-close.js has a new test: no error when closing before the response arrives.

Without the fetch() change, the hook, Pool-queue, TLS-hang, never-started-retry, WebSocket and EventSource tests fail. Without the EventSource fix, both EventSource tests fail.

Suites: unit 1724/1727, fetch (with webidl and busboy) 607/607, websocket 178/178, eventsource 154/154, cache 5/5, node-test 245/250, interceptors 513/516, cache-interceptor 87/87. No failures; the rest are skipped. Lint is clean.

WPT /eventsource, /websockets and /fetch, run locally against the same WPT checkout on #6221 and on this branch: 3,865 passing and 2,944 failing on both, with the same status for every test. The failures are expected in my local setup; only the comparison matters here.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Sfj4nCVnJiJpsfbRdvDxpz

@codecov-commenter

codecov-commenter commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.08197% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.69%. Comparing base (ab93025) to head (c9038d6).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/web/fetch/index.js 93.87% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6223      +/-   ##
==========================================
- Coverage   91.70%   91.69%   -0.02%     
==========================================
  Files         113      113              
  Lines       44820    44874      +54     
==========================================
+ Hits        41104    41146      +42     
- Misses       3716     3728      +12     

☔ 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
mcollina requested review from KhafraDev and ronag November 19, 2026 14:45
@mcollina
mcollina force-pushed the fix/cancel-abandoned-connect branch from d85e2fe to 249dfc3 Compare November 19, 2026 14:46
Base automatically changed from fix/cancel-abandoned-connect to main November 19, 2026 14:46
mcollina and others added 2 commits November 20, 2026 11:38
close() aborts the fetch of the EventSource. When its request was still
in flight, the aborted network error then failed the connection and
fired 'error', although readyState was already CLOSED. The spec only
fires 'error' when failing a connection whose readyState is not CLOSED.

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>
fetch() did not pass the queued-abort hook, so a fetch aborted while it
waited for a connection stayed in the Pool or Client queue until a
connection was ready. Its connection attempt kept running too: the
process stayed alive until the connect timeout, and the pool slot
stayed taken.

A fetch that can be aborted now passes the hook. Until its request
starts, aborting or terminating the fetch takes it out of the queue,
and a connection attempt that no request waits for any more is
destroyed.

- fetch() passes the hook only when its Request follows a signal:
  without one, nothing can abort it before it starts, so it costs
  nothing.
- fetching() passes it by default. WebSocket and EventSource abort
  their fetch on close(), and Cache.addAll() aborts the other fetches
  when one fails.
- The hook chains with the abort of an attempt that already started,
  like the one for the other APIs, for interceptors such as retry.

Fixes: #4405
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 force-pushed the fix/fetch-abort-queued branch from fcbc58e to c9038d6 Compare November 20, 2026 10:39

This branch has not been deployed

No deployments
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.

Process does not exit after aborting a request to an unresponsive host

2 participants