Skip to content

fix(octane): pace Suspense retries on an already-settled wakeable - #1400

Merged
trueadm merged 2 commits into
mainfrom
claude/laughing-mendel-7c83b8
Sep 29, 2026
Merged

trueadm merged 2 commits into
mainfrom
claude/laughing-mendel-7c83b8

Conversation

@trueadm

@trueadm trueadm commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A Suspense retry that suspended again on a promise whose settlement Octane had already observed re-rendered on that promise's next microtask, forever. Two cases hit this: a resource reader that throws a resolved promise before its own state catches up, and use() of a thenable whose status React does not recognize, such as router-core's 'resolved'. Both React and Octane treat that status as pending and leave the thenable uninstrumented. Timers and network callbacks never ran, so the state that would end the suspension never arrived, and the page livelocked at 100% CPU. React sends the same retry through a Scheduler task, so the component spins without starving the event loop. #1397 avoided this in the tanstack-router binding; this PR fixes it in core.

Every client resume subscription now goes through one helper, resumeOnSettle in runtime.ts: boundaries, transition holds, roots without a boundary, hidden Activity, passthrough pending, and initial and <Hydrate> hydration.

  • First settlement: the retry still runs on the settling microtask, so ordinary data reveals exactly as before.
  • Settlement already observed: tracked in a lazy WeakSet and checked when subscribing. The helper still subscribes through then, so a reusable custom wakeable waits for its next notification instead of being polled. It then retries after one macrotask (setTimeout(retry, 0)).
  • use(): positional semantics and its handling of unrecognized statuses are unchanged.

Decision: yielding a task was chosen over a retry bound. A bound either fails a legitimate component or still spins until the bound. Yielding lets the callback that ends the suspension run, matches React, and costs nothing on paths that never loop.

Docs: packages/octane/audit/SUSPENSE_DIVERGENCE.md §13 and docs/differences-from-react.md. Includes a patch changeset for octane.

Tests

The new test cases open their gate from a timer. Each gate also opens itself after 2,000 checks and records that it did, because the pre-fix loop would otherwise starve Vitest's own timeout.

  • suspense-settled-wakeable.test.ts (new):
    • resolved and rejected thrown promises through template and JSX boundaries;
    • use() of a 'resolved'-status thenable;
    • an initial root without a boundary;
    • boundary and root transition holds;
    • hidden Activity;
    • a reusable custom wakeable that must not be polled;
    • two controls that must still reveal on microtasks alone: a resolved promise thrown for the first time, and one pending promise shared by two roots.
  • initial-suspense-hydrate.test.ts: initial adoption waiting on a settled gate (both gate placements).
  • deferred-hydration-contract.test.ts: <Hydrate> activation reading a 'resolved'-status thenable.

Pre-fix failures: 8 of the new cases fail on main. Reverting just the initial-adoption subscription fails its 2 hydration cases, and reverting just the <Hydrate> activation subscription fails its case. Each plausible wrong design turns a test red:

  • deciding when the promise settles, instead of when subscribing, delays the second root sharing one pending promise;
  • skipping then for known-settled wakeables polls the reusable wakeable;
  • pacing every retry breaks both microtask controls.

Validation

  • pnpm format:files:check on every changed file (not the repo-wide pnpm format:check)
  • Typecheck: tsgo --noEmit -p packages/octane/tsconfig.json, plus pnpm typecheck:files on the runtime, the new test and its fixture (not the repo-wide pnpm typecheck)
  • pnpm test: not run whole locally; it runs out of memory on this machine. CI covers it.
  • targeted tests:
    • Before merging main: the full octane and octane-prod projects, 24,617 of 24,627 passed. Most of the 10 failures were 5 s compile or import timeouts under load average ~45 from other local sessions. The HMR, behavior-root and independent-parent files passed when re-run in isolation, and the HMR test passed three times in a row. The volar compile-timeout test was not re-run.
    • Before merging main: 20 Suspense-heavy binding projects (tanstack-router/-differential/-ssr, tanstack-query/-differential, jotai/-differential/-hydration, swr/-differential, apollo-client/-differential, i18next/-differential, livestore, remix-router/-differential, tanstack-start, tanstack-db, lexical), 1,169 of 1,170 passed. The one failure was jotai's export-surface 5 s timeout, which passed in isolation.
    • After merging main: 56 file runs, 1,391 tests, all passed. They cover the new and changed tests plus the nearest Suspense, SSR-Suspense, hydration, portal and actions files in both octane and octane-prod, and the tanstack-router (including fix(tanstack-router): keep route matches suspended across match states #1397's match-suspension tests), livestore and i18next packages.
  • Performance evidence:
    • Bundle (benchmarks/bundle-size/run-minimal.mjs): +109 B raw and about +45 B gzip on client bundles that include root rendering. Server, capture-only and vanilla-binding bundles are unchanged, and every scenario stays under its budget.
    • audit-981-coverage Suspense list (64 independently pending keyed boundaries, compared with the baseline source via CLIENT_SOURCE_ROOT): function-entry counts are identical on all 12 operations, including settle-odd and settle-even. All 68 applicable ratio guards pass. The render path is untouched.
    • Isolated subscription cost: about +100 ns per suspension (≈40 → 140 ns in batches of 100), almost all of it the WeakSet insert. One boundary's suspend-and-resume costs about 125 µs in jsdom, so this is roughly 0.1%. A symbol property on the promise would be cheaper, but it writes to user-owned thenables that proxies can observe and frozen objects reject; React also tracks thrown wakeables in a WeakMap/WeakSet. Not per render.
    • A jsdom wall-clock A/B was inconclusive under load and is not claimed.

Risk / follow-ups

  • The record is per wakeable. A reader that throws a new already-settled promise on every attempt still retries on microtasks. React spins there too, across tasks. Realistic libraries (router-core, cache-backed readers, use() replay) reuse the same promise.
  • A paced retry is a timer that act() does not drain, like the retry-reveal delay in §5. A test has to await a task inside act() to observe it.
  • The passthrough-pending subscription has no dedicated test, because it needs a list above a selected hydration container. It uses the same helper as the covered paths.
  • Not in this PR:
    • SSR prerender stops with "exceeded 50 suspense passes" after 51 reads, before a 0 ms timer can fire. A probe confirmed this, and the error message wrongly blames use().
    • universal-core.ts resubscribes the same microtask way. It is very likely affected, but not confirmed here.

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 Suspense scheduling across boundaries, roots, Activity, and hydration; behavior change is intentional but widespread, with small client bundle growth and paced retries not fully drained by act() without awaiting a task.

Overview
Fixes a Suspense livelock where a retry that kept suspending on a promise Octane had already seen settle re-ran on that promise’s microtask forever, starving timers and network callbacks so the UI never recovered. Typical triggers are resource readers that throw a resolved promise before local state catches up, or use() on thenables with unrecognized status values (e.g. router-core’s 'resolved').

resumeOnSettle in runtime.ts now backs every client Suspense resume subscription (boundaries, root holds, transition/hidden Activity, passthrough pending, initial and deferred hydration). The first settlement still schedules the retry on the settling microtask; after a wakeable is recorded in a lazy WeakSet, further retries still subscribe via then but yield one macrotask (setTimeout(0)) after the next notification, matching React’s scheduler pacing without polling custom wakeables.

Docs add SUSPENSE_DIVERGENCE §13 and a differences-from-react note; a patch changeset ships the fix. New suspense-settled-wakeable tests (plus hydration cases) assert timer-driven recovery and that first-time / shared-pending paths still reveal on microtasks.

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

trueadm and others added 2 commits September 29, 2026 15:15
A retry that suspended again on a promise whose settlement had already been
observed re-rendered on that promise's next microtask, forever. This happens
when a resource reader throws a resolved promise before its own state catches
up, or when use() reads a thenable whose status React does not recognize
(router-core reports 'resolved'), which both runtimes treat as pending. Timers
and network callbacks never ran, so the state that would end the suspension
never arrived and the page livelocked.

Every client resume subscription now goes through resumeOnSettle. A first
settlement still retries on its microtask. A wakeable whose settlement was
already seen still subscribes through then, so a reusable custom wakeable is
not polled, and then retries after one macrotask, as React's Scheduler ping
does. use() semantics are unchanged.

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
@github-actions
github-actions Bot marked this pull request as draft September 29, 2026 14:17
@trueadm
trueadm marked this pull request as ready for review September 29, 2026 14:42
@trueadm
trueadm merged commit 28a3636 into main Sep 29, 2026
59 checks passed
@trueadm
trueadm deleted the claude/laughing-mendel-7c83b8 branch September 29, 2026 15:01
trueadm added a commit that referenced this pull request Sep 29, 2026
With #1400 on main, a universal root inside a DOM Suspense boundary is paced by
both runtimes' resume subscriptions. Cover it directly: a projected root
suspension, and one that holds a transition. The second fails if the urgent
projection is released on the microtask, which is the only regression test for
that pacing. Link the universal contract from SUSPENSE_DIVERGENCE §13.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm added a commit that referenced this pull request Sep 29, 2026
…1404)

* fix(universal): pace Suspense retries on an already-settled thenable

A universal retry that suspended again on a thenable whose settlement had
already been observed re-rendered on that thenable's next microtask, forever.
This happens when a thenable's owner publishes its status on a later task, or
when a reverse-region DOM child routes the same suspension back to its universal
owner on every commit. Timers and native callbacks never ran, so the state that
would end the suspension never arrived and the root livelocked. Boundary and
hidden-Activity replays, root and transition suspensions, and routed renderer-
region suspensions all looped.

Every universal resume subscription now goes through resumeOnSettle, the same
design as the DOM runtime. A first settlement still retries on its microtask. A
thenable whose settlement was already seen still subscribes through then, so a
reusable custom wakeable is not polled, and then retries after one macrotask, as
React's Scheduler ping does. The urgent-projection release is paced the same
way: releasing it on the microtask lets a DOM-hosted root with held transition
work re-prepare, suspend on the same thenable, and loop.

Universal use() also stops rewriting a thenable status it did not set. It used
to replace a status React does not recognize, such as router-core's
'resolved', with its own tracking. It now leaves it alone and treats the
thenable as pending, matching React's trackUsedThenable and the DOM runtime.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(universal): cover a DOM-hosted universal root pacing both runtimes

With #1400 on main, a universal root inside a DOM Suspense boundary is paced by
both runtimes' resume subscriptions. Cover it directly: a projected root
suspension, and one that holds a transition. The second fails if the urgent
projection is released on the microtask, which is the only regression test for
that pacing. Link the universal contract from SUSPENSE_DIVERGENCE §13.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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