Skip to content

fix(tanstack-query): keep queries after useSuspenseQueries suspended - #1390

Merged
trueadm merged 3 commits into
mainfrom
fix/tanstack-query-grouped-suspense
Sep 29, 2026
Merged

trueadm merged 3 commits into
mainfrom
fix/tanstack-query-grouped-suspense

Conversation

@trueadm

@trueadm trueadm commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Closes #1381.

A component that reads a group with useSuspenseQueries() and then another suspense query could render the second query's data as undefined while that query was still pending. With a string read such as b.data.toUpperCase(), the render threw into @catch, and the error stayed after both queries resolved.

Owner: the binding, not core. Octane's use() is positional and, on a resume replay, keeps the thenable already stored at an occupied position. React 19.2.7's trackUsedThenable does the same: it looks thenables up by index and reuses the previous one. So a hook whose use() disappears between a suspended attempt and its replay shifts every later use() in React too. @tanstack/react-query never hits this because it throws promises, which have no position. Core stays as is.

Root cause. useSuspensePromise (in internal.ts) exists to keep reading the promise that suspended a hook call site after it settles, which reserves that call site's use() position for the replay. useQueries keyed that retention by the queries still pending. Once the group settled, the key became [], the useMemo dropped the settled promise, and the group skipped its use(). The following query's use() then replayed the group's fulfilled promise and fell through with pending data.

Fix. useQueries now keys the retained promise by every suspense query's hash, so settling no longer changes the key. useBaseQuery already keys by queryHash, which is stable through settling, so it needed no change. #943's key-specific retention is preserved: a held transition still restores the committed group's promise. The key contract is now documented on useSuspensePromise.

I considered always calling use() with a module-level settled thenable when nothing is retained, and did not do it. The same-thenable replay path already covers this bug. A sentinel would not help a call site that becomes pending mid-replay, because the stored thenable at that position still wins. It would also trigger the dev "uncached promise" warning when a position's thenable switches, and add one hydration seed per suspense hook to SSR output.

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm test: not run in full. I ran the tanstack-query projects and every package that depends on @octanejs/tanstack-query, listed below.
  • targeted tests:
    • pnpm vitest run --project tanstack-query --project tanstack-query-differential --project tanstack-query-ssr --project tanstack-query-adapted --project tanstack-query-adapted-ssr --project tanstack-query-pristine: 456/456 passed.
    • The tanstack-query conformance suite compiled with octane({ hmr: false }) (scratch config): 44/44 passed.
    • The dependents (wagmi, wagmi-differential, rainbowkit, rainbowkit-differential, rainbowkit-ssr, solana-kit, solana-kit-adapted, solana-kit-pristine, tanstack-router-ssr-query, tanstack-router-ssr-query-differential, tanstack-router-ssr-query-parity-audit): 153/153 passed.
    • pnpm react-parity:validate passes; the manifest now lists the new cases and updated hashes.
    • The issue's own reproduction (fixture, config and React controls) now passes in both the development and EVIDENCE_PROD=1 runs, 6/6 each. Before the fix it failed the two grouped Octane cases in both modes.
    • The new tests fail before the fix in both development and hmr: false compilation, and pass after it:
      • Conformance (followups.test.ts): the grouped→singular and grouped→grouped cases fail; the singular→singular control passes both before and after.
      • Differential against @tanstack/[email protected] and React 19.2.7, run in development compilation: grouped→singular and grouped→grouped diverge at the "first settled, second pending" step. Octane renders TypeError: …reading 'toUpperCase' where React renders loading. The singular→singular control matches. With the fix, all 6 differential cases pass, and passed 3 repeated runs.
    • Grouped transition hold (transition-suspense.test.ts): passes before and after, because it guards fix: address React behavioral audit defects #943's behavior rather than this bug. A plausible wrong fix, a group-wide constant key, fixes the sequential cases but fails this test by showing the fallback during the held transition.
    • pnpm typecheck:files on the changed files reports the same errors before and after, all already present on main in followups.tsrx (client: unknown props and Element/ReactNode under the React-JSX program). This PR adds none.

Risk / follow-ups

  • Low risk. The change only affects the retention key for useQueries/useSuspenseQueries. Non-suspense useQueries gets a constant "[]" key and never retains a promise.
  • The differential cases wait past React's 300 ms throttle on revealing content after a fallback. B settles 200 ms after A to leave headroom before the "first settled" snapshot on a loaded runner.
  • Other bindings have the same defect class but a different mechanism: a conditional use() whose condition flips when data settles, so a following use() in the same component can take its position. None of them uses this retained-promise helper; tanstack-db already calls use() unconditionally with a settled thenable. These cases were found by reading the code; none has a reproduction yet, and they are not changed here:
    • packages/i18next/src/useTranslation.js (if (i18n && useSuspense && !ready) use(...)). react-i18next throws the promise instead.
    • packages/livestore/src/useStore.ts (storeOrPromise instanceof Promise ? use(storeOrPromise) : storeOrPromise). Upstream has the same conditional React.use, so a second useStore could receive the first store.
    • packages/tanstack-router/src/Match.tsrx MatchInner (the _forcePending → status === 'pending' use() sites share one component's positions). Upstream throws promises.
    • Narrower: packages/swr promise fallbackData (use(fallback) only while the cache is empty).

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Low Risk
Narrow change to suspense promise retention keys in useQueries; non-suspense useQueries is unaffected, and new tests lock sequential suspense and transition behavior.

Overview
Fixes Octane suspense replay when a component uses useSuspenseQueries() and then another suspense hook in the same render.

useQueries now keys the retained useSuspensePromise memo by every suspense query’s queryHash, not only hashes that are still pending. When the group settled, the old key collapsed and the group stopped calling use(), so a following useSuspenseQuery / useSuspenseQueries could replay the group’s fulfilled thenable and render with data undefined (often stuck in @catch). useSuspensePromise docs now state that the key must stay stable through settling.

Tests add sequential suspense conformance and React differential scenarios (useSuspenseQueries → singular/grouped), plus a useSuspenseQueries transition-hold case; audit manifests and a patch changeset are updated.

Reviewed by Cursor Bugbot for commit ce1a760. Bugbot is set up for automated code reviews on this repo. Configure here.

useSuspenseQueries retained its suspense promise under a key made of the
queries still pending. Once the group settled that key became `[]`, the
memo dropped the settled promise, and the hook stopped calling use() on the
replay. use() is positional, so a following useSuspenseQuery or
useSuspenseQueries in the same component landed on the group's fulfilled
position, did not suspend, and rendered with `data` undefined, throwing into
the error boundary.

Key the group's retained promise by every suspense query it reads, as
useBaseQuery already keys by its query hash. Add grouped-to-singular and
grouped-to-grouped conformance regressions next to the singular control,
a grouped transition hold that rejects a group-wide constant key, and a
React differential control for all three orders.

Closes #1381

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Sep 29, 2026
@trueadm
trueadm marked this pull request as ready for review September 29, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useSuspenseQueries lets a following suspense query render undefined data and enter the error boundary

1 participant