fix(query-core): scope abort signal consumption to each fetch 🤖🤖🤖 #12717
main fromrefs/pull/12717/head·opened View on GitHub🎯 Changes#
Fixes #12716.
Scope abort-signal consumption to the fetch that created the signal. A hydrated promise starts with its own unread signal state. If a superseded fetch reads its signal later, it only changes that fetch's state. The existing reset between ordinary retry attempts stays in place.
Added deterministic tests for both paths. Each test fails on main at 635aad6 without the fix and passes with it. Local verification covered 672 query-core runtime tests, TypeScript checking for query-core source and tests, formatting for the changed TypeScript files, and git diff --check. The full pnpm run test:pr was unavailable in the isolated checkout because its pinned pnpm was missing. Preview size measurements are pending.
Automated submission by Codex on behalf of 0718lol.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I have followed the AI contribution policy and fully understand the code in this pull request, including any code generated with AI assistance.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Fixed cancellation handling for replacement requests, ensuring a newer fetch can complete even if an earlier request is canceled.
- Fixed hydration of pending queries so completed server results are retained instead of triggering an unnecessary client-side fetch.
- 1
0718lol(GitHub profile, opens in a new tab) linked this to fix #12716
tanstack-source added labelsbug
No actionable comments were generated in the recent review.
Walkthrough
-
Query now tracks abort-signal consumption per fetch. Observer removal uses the current fetch's state when deciding whether to cancel the fetch or only stop retries. Regression tests cover replacement fetches and hydrated pending queries.
Changes: Per-fetch abort-signal tracking
Layer / File(s) Summary Track abort-signal consumption per fetch
packages/query-core/src/query.tsEach fetch gets its own abort-signal state. Reading the signal marks that fetch's state as consumed. Observer removal checks the current fetch's state when deciding how to cancel. Cover replacement and hydrated fetches
packages/query-core/src/__tests__/query.test.tsx,packages/query-core/src/__tests__/hydration.test.tsx,.changeset/scope-query-abort-signal.mdThe tests cover a late signal read during overlapping refetches and hydration of a pending query. The changeset records a patch for @tanstack/query-core.Estimated code review effort: 2 (Simple) | ~12 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
tkdodo,sukvvon
Merge Risk:
Minimal · up to66ef1-
This change scopes abort-signal tracking to the fetch that created it. Unsubscribing no longer wrongly cancels a replacement or hydrated fetch because of an earlier fetch's late signal read. The change is small and confined to query-core, and the source trace found no regression, so it carries minimal merge risk.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the query-core fix: abort signal consumption is scoped to each fetch. It matches the main change in query.ts and the related tests. The emoji suffix is noise, but the title stays speci… Description check The description covers the change, the linked issue ( #12716), and the local verification. All three checklist items and the release-impact section are present. The author ticked the changeset box, and…Linked Issues check Objective [ #12716]: a hydrated pending query must cache its server result when its own signal was never read and the last observer unsubscribes. The head replaces the Query-wide#abortSignalConsumed…Out of Scope Changes check All changes support the #12716objective. query.ts changes replace the shared abort-signal flag with per-fetch state, and the reset between retry attempts is preserved. hydration.test.tsx adds a test …Full details: Title check
-
Explanation
The title names the query-core fix: abort signal consumption is scoped to each fetch. It matches the main change in query.ts and the related tests. The emoji suffix is noise, but the title stays specific and accurate.
Full details: Description check
-
Explanation
The description covers the change, the linked issue (
#12716), and the local verification. All three checklist items and the release-impact section are present. The author ticked the changeset box, and the raw summary confirms a changeset exists. The checklist items are unticked, but the template allows that when the tests may not apply. The description also says pnpm run test:pr was unavailable, which the author disclosed.
Full details: Linked Issues check
-
Explanation
Objective [
#12716]: a hydrated pending query must cache its server result when its own signal was never read and the last observer unsubscribes. The head replaces the Query-wide#abortSignalConsumedboolean with#abortSignalState, which isundefineduntil a client fetch assigns it (query.ts,#abortSignalState = abortSignalStatebeforecreateRetryer). InremoveObserver, the checkthis.#abortSignalState?.consumedevaluates falsy for a hydrated promise, so the retryer only stops retries and the fetch finishes. The hydrated result is therefore cached. Objective [#12716], inverse case: each fetch creates its ownabortSignalStateobject. The signal getter setsconsumedonly on the state captured by its own fetch. A late read from a superseded fetch therefore cannot change the state that governs the replacement fetch. The retry reset (abortSignalState.consumed = falsein the per-attempt wrapper) keeps the prior behavior for ordinary retries. The added hydration test in hydration.test.tsx covers the first path, and the query.test.tsx regression test covers the second. The PR does not implement the related#11532work, which the issue does not require. I did not run the test suite. The evidence is the source diff and the test diff, plus the author's report that each new test fails on main.
Full details: Out of Scope Changes check
-
Explanation
All changes support the
#12716objective. query.ts changes replace the shared abort-signal flag with per-fetch state, and the reset between retry attempts is preserved. hydration.test.tsx adds a test for the hydrated pending path and the imports it needs (QueryObserver,QueryFunction). query.test.tsx adds the late-signal-read regression test. The changeset is a patch note for the published query-core fix. No unrelated files or behavior changes appear in the summary or the inspected diffs.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Warning
⚠️ This pull request shows signs of AI-generated slop (ai_padded_prose). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/query
- Branch: main
- File: .coderabbit.yaml
- Commit: 635aad6
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
613f1ee6-b4cb-4bfb-bb4b-042a16300b24
Commits-
Reviewing files that changed from the base of the PR and between 635aad6a95b08dffc91ba1973c043b219f085cd6 and 66ef16ed5f2e143e534ae5211ac455dd57490751.
Files selected for processing: 4-
.changeset/scope-query-abort-signal.mdpackages/query-core/src/__tests__/hydration.test.tsxpackages/query-core/src/__tests__/query.test.tsxpackages/query-core/src/query.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
-
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 66ef16ed5f2e143e534ae5211ac455dd57490751
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 26 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR