Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core): scope abort signal consumption to each fetch 🤖🤖🤖 #12717

Open0718lol(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/12717/headopened View on GitHub

0718lol(GitHub profile, opens in a new tab)commented

🎯 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. 🦋 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
      NameType
      @tanstack/query-corePatch
      @tanstack/angular-queryPatch
      @tanstack/lit-queryPatch
      @tanstack/preact-queryPatch
      @tanstack/query-async-storage-persisterPatch
      @tanstack/query-broadcast-client-experimentalPatch
      @tanstack/query-devtoolsPatch
      @tanstack/query-persist-client-corePatch
      @tanstack/query-sync-storage-persisterPatch
      @tanstack/react-queryPatch
      @tanstack/solid-queryPatch
      @tanstack/svelte-queryPatch
      @tanstack/vue-queryPatch
      @tanstack/angular-query-devtoolsPatch
      @tanstack/angular-query-persist-clientPatch
      @tanstack/preact-query-devtoolsPatch
      @tanstack/preact-query-persist-clientPatch
      @tanstack/react-query-devtoolsPatch
      @tanstack/solid-query-devtoolsPatch
      @tanstack/svelte-query-devtoolsPatch
      @tanstack/vue-query-devtoolsPatch
      @tanstack/react-query-persist-clientPatch
      @tanstack/solid-query-persist-clientPatch
      @tanstack/svelte-query-persist-clientPatch
      @tanstack/react-query-next-experimentalPatch
      @tanstack/eslint-plugin-queryPatch

      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

    2. tanstack-source added labelsbug

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →

      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.ts
      Each 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.md
      The 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 to 66ef1

      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: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe 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 checkPassedThe 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 checkPassedObjective [#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 checkPassedAll changes support the #12716 objective. 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 #abortSignalConsumed boolean with #abortSignalState, which is undefined until a client fetch assigns it (query.ts, #abortSignalState = abortSignalState before createRetryer). In removeObserver, the check this.#abortSignalState?.consumed evaluates 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 own abortSignalState object. The signal getter sets consumed only 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 = false in 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 #11532 work, 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 #12716 objective. 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.

      Share

      Recent review info

      Run configuration

      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.md
      • packages/query-core/src/__tests__/hydration.test.tsx
      • packages/query-core/src/__tests__/query.test.tsx
      • packages/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.