Skip to content

fix(tanstack-router): keep route matches suspended across match states - #1397

Merged
trueadm merged 2 commits into
mainfrom
fix/tanstack-router-stable-match-suspension
Sep 29, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/tanstack-router-stable-match-suspension

Conversation

@trueadm

@trueadm trueadm commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

MatchInner in @octanejs/tanstack-router suspended through use(), on a different router promise for each match state: displayPendingPromise, minPendingPromise, or loadPromise for pending and redirected. Octane's use(thenable) is positional. During a resume replay, a thenable at an occupied position is replaced by the stored one, the same as React 19's trackUsedThenable. So when a suspended match changed state, the replay got back the promise stored for the earlier state.

The suspected failure was a render without loader data. With router-core's real promises it is a livelock instead. createControlledPromise settles to status: 'resolved', not React's 'fulfilled', so use() reads the stored promise as still pending. Every retry suspends on it again, the settled promise wakes the boundary on the next microtask, and the loop starves the timers and network callbacks the loader needs to finish.

Two consumer paths hit it:

  • Hydrated ssr: false route with a slow client loader. router-core's hydrate() sets _forcePending with a minPendingPromise. When pendingMinMs elapses before the client loader resolves, the replay's use(loadPromise) lands on the stored, settled minPendingPromise and the page locks up.
  • Loader redirect to a route whose loader waits on a timer or the network. handleRedirectAndNotFound resolves the match's loadPromise when it marks the match redirected. use() then re-suspends on that settled promise on every microtask until the redirect target commits, and the target cannot load while the loop runs.

Related: #1381, fixed for tanstack-query in #1390.

Changes

  • MatchInner now throws the relevant promise instead of calling use(), as upstream does. A thrown promise has no position, so a replay always suspends on the promise for the current state. Core use() semantics are unchanged.
  • A redirected match waits for its next match-store update instead of its loadPromise, which router-core has already resolved. That promise would otherwise retry on microtasks until the redirect target loads, starving that load's timers and network callbacks. React's scheduler paces the same retry for upstream; Octane's retry does not. Server atoms have no subscribe, so the server keeps upstream's loadPromise; Start answers server redirects before rendering.
  • New regression tests, tests/conformance/match-suspension.test.ts with fixture tests/_fixtures/match-suspension.tsrx:
    • Hydrates through the real router-core hydrate() and RouterClient with an ssr: false route whose client loader outlasts pendingMinMs.
    • Navigates to a route whose loader redirects to a route with a 30ms timer loader.
    • Both assert the route component never renders before its loader data exists, and that the final content renders.
    • A render-loop guard caps router.getMatch reads, so a regression fails in about a second instead of hanging the worker. A test timeout cannot fire during a microtask livelock.
  • Classified the new test file in packages/tanstack-router/audit/test-classifications.json.
  • Patch changeset for @octanejs/tanstack-router.

Validation

  • pnpm format:check (not run repo-wide; pnpm format:files:check on the changed files passes)
  • pnpm typecheck (not run repo-wide; pnpm typecheck:files on the changed files passes via tsrx-tsc)
  • pnpm test (not run in full; targeted projects below)
  • targeted tests, dev compile: vitest run --project tanstack-router --project tanstack-router-ssr --project tanstack-router-differential --project tanstack-router-ssr-query, 19 files / 105 tests passing
  • targeted tests, hmr: false: the tanstack-router project re-run through a scratch config with octane({ hmr: false }), 16 files / 89 tests passing
  • pnpm react-parity:validate, pnpm changeset:check, and pnpm sync (clean)
  • Pre-fix failure checked in dev and hmr: false: against the original use() code, both new tests trip the render-loop guard; without the guard the worker spun at 100% CPU until killed. Two other broken variants also fail. A literal upstream port that throws loadPromise for redirected fails the redirect test the same way. A single use() on the currently relevant promise at one fixed position fails both tests, because the stored settled promise still wins on the replay.

Risk / follow-ups

  • nextMatchUpdate subscribes once per redirected match snapshot, cached in a WeakMap. If the match unmounts without another store update, the listener stays on the orphaned atom and is collected with it.
  • Octane core retries a suspension on a settled thenable on the next microtask, where React paces the retry through its scheduler. A component that keeps throwing a settled promise starves macrotasks. This PR avoids that in the binding; any core pacing change is out of scope here.
  • Noticed, not addressed: after RouterClient hydrates an ssr: false route in jsdom (createRoot), the root Matches Suspense stays in fallback with the root layout hidden (display: none) while the child route is pending. This happens both before and after this change; client navigation does not show it.

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

Medium Risk
Touches core route match Suspense/navigation rendering; behavior change is narrow and regression-tested, but incorrect suspension could still affect loading, redirects, and hydration.

Overview
Fixes page livelocks and premature route renders when a TanStack route match moves between pending states (hydration force-pending, slow loaders, redirects).

MatchInner no longer suspends via Octane's positional use(thenable). It throws the same router-core promises as upstream for _displayPending, _forcePending, and pending, so Suspense retries always wait on the promise for the current state instead of reusing a settled promise from an earlier state (which could spin on microtasks and block timers/network).

For redirected, suspension waits on nextMatchUpdate (first match-store update after the snapshot) when the store supports subscribe, instead of loadPromise, which router-core has already resolved—avoiding the same microtask loop while the redirect target's loader runs.

Adds conformance tests (hydrated ssr: false client loader outlasting pendingMinMs, redirect to a timer-based target), a shared fixture, audit classification, and a patch changeset.

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

MatchInner suspended through use() on a different router promise per match
state. use() is positional, so after a state change a replay received the
promise stored for the earlier state. Router-core's controlled promises
settle to status 'resolved', which use() reads as pending, so the stored
promise re-suspended on every retry, on a microtask, starving the timers and
network callbacks the loader needed.

Throw the relevant promise instead, as upstream does. A redirected match
waits for its next store update rather than its already-resolved
loadPromise, which would otherwise spin until the redirect target loads.

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 13:26
@trueadm
trueadm merged commit b1d7e24 into main Sep 29, 2026
44 of 47 checks passed
@trueadm
trueadm deleted the fix/tanstack-router-stable-match-suspension branch September 29, 2026 13:50
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.

1 participant