Skip to content

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

Merged
trueadm merged 3 commits into
mainfrom
claude/quirky-varahamihira-61f669
Sep 29, 2026
Merged

trueadm merged 3 commits into
mainfrom
claude/quirky-varahamihira-61f669

Conversation

@trueadm

@trueadm trueadm commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The universal renderer (octane/universal, used by Lynx, three, ink and opentui) had the same Suspense retry livelock that #1400 fixed in the DOM runtime. A retry that suspended again on a thenable whose settlement had already been observed re-rendered on that thenable's next microtask, forever. Timers and native callbacks never ran, so the state that would end the suspension never arrived.

It is reachable when a thenable's owner publishes its status on a later task. It is also reachable when a reverse-region DOM child re-suspends on every host commit and routes the suspension back to its universal owner. Boundary and hidden-Activity replays, root suspensions without a boundary, held transitions, routed renderer-region suspensions, and universal roots inside a DOM Suspense boundary all looped.

Changes

  • packages/octane/src/universal-core.ts adds resumeOnSettle, the same design as fix(octane): pace Suspense retries on an already-settled wakeable #1400. A lazy WeakSet records wakeables whose settlement a resume listener has seen, and the decision is made when the listener is attached. A first settlement still retries on its microtask. A thenable already seen to settle still subscribes through then, so a reusable custom wakeable is not polled, and then retries after one macrotask, as React's Scheduler ping does.
  • It routes every universal resume listener through it: the root suspended-attempt retry, publishLocalReplay (boundary and hidden Activity), routeUniversalOwnerSuspense (reverse-region DOM children), and releaseProjection in suspend(). The last one matters. Released on the microtask, it lets a DOM-hosted root with held transition work re-prepare and suspend on the same thenable again.
  • Deliberate use() change: trackUniversalThenable no longer rewrites a status it did not set. It used to replace an unrecognized status, such as router-core's 'resolved', with its own 'pending'/'fulfilled' tracking, which writes to a field another library owns. History shows no rationale: it arrived with the Lynx scaffolding (Add Lynx Phase 1 native scaffolding #180), with no comment, doc or test. It now leaves the status alone and treats the thenable as pending, matching React's trackUsedThenable and the DOM runtime. The visible change: universal use() of a 'resolved'-status thenable stays suspended, with paced retries, instead of revealing after one microtask. The changeset calls this out.
  • Docs: a universal paragraph in SUSPENSE_DIVERGENCE.md §13, and the contract in docs/universal-renderer-architecture.md §6.
  • The branch merges main, including fix(octane): pace Suspense retries on an already-settled wakeable #1400. A universal root inside a DOM boundary resumes through both runtimes, so its tests need both fixes.

Validation

  • pnpm format:check: not run repo-wide; pnpm format:files:check on every changed file passes. pnpm sync makes no changes.
  • pnpm typecheck: not run in full; tsgo -p packages/octane/tsconfig.json, tsgo -p packages/lynx/tsconfig.json, tsgo -p packages/lynx/tsconfig.testing.json and tsrx-tsc -p packages/three/tsconfig.json all pass.
  • pnpm test: not run in full. The projects this change reaches were run on the merged head instead:
    • octane + octane-prod: 24,659 / 24,661 pass. Under full-suite load, two behavior-root.test.ts textarea-retirement tests hit the 5 s timeout. In an earlier run it was one hydration/independent-parent.test.ts activation-timing assertion. Neither file touches universal code, and both pass 478/478 in isolation on repeated runs in both projects.
    • lynx (404), three + three-differential (142), ink + ink-differential (30), opentui (4): all pass.
  • targeted tests: new tests/universal-settled-wakeable.test.ts, 12 tests, run in both octane and octane-prod. It uses a compiled .object.tsrx fixture, a hand-written reverse-region scene, and the existing DOM @pending host-boundary fixture.
    • Each livelock gate opens from a timer, and opens itself after 2,000 checks so a microtask loop can't hang Vitest. Seven tests fail on the old universal core: boundary (resolved and rejected), 'resolved' status, root, transition, hidden Activity, and routed renderer region. Both DOM-hosted tests also fail there.
    • Controls: a first settlement still reveals on microtasks, one pending promise shared by two roots too, and a reusable custom wakeable is not polled.
    • Mutation checks: reverting each site turns a specific test red. That covers the attempt, the replay, the routed path, releaseProjection (caught only by the DOM-hosted held-transition test) and trackUniversalThenable. So does each rejected design: deciding when the listener fires, skipping then for settled thenables, and pacing every retry.
  • Bundle size, measured before and after:
    • node benchmarks/bundle-size/run-minimal.mjs: identical across every scenario. None of them reaches universal-core.
    • Direct esbuild production builds: universal-core everything-exported raw +0 / gzip +36 / brotli +104. A typical universal consumer raw +0 / gzip +40 / brotli +40. Lynx index raw +50 / gzip +35 / brotli −85. Raw +0 is real: the helper roughly offsets the removed status checks and shorter call sites.
  • Runtime: +~120 ns per suspension for the subscribe step, measured in isolation. It runs only when a component suspends, never per render or per node. Full suspend-and-reveal timings were too noisy to show a difference.

Risk / follow-ups

  • A paced retry is a timer, so flushUniversalAct does not drain it, the same as act() for fix(octane): pace Suspense retries on an already-settled wakeable #1400. No existing Lynx, three, ink or opentui test relied on microtask re-suspension converging.
  • Still open, not covered here:
    • useBatch makes a fresh combined promise for two or more pending members, so a batch of two or more lagging-status thenables still retries on microtasks. The DOM runtime has the same gap.
    • The Lynx first-screen renderer (packages/lynx/src/main-renderer.ts) keeps its own copy of the status overwrite. It renders once and never retries, so it cannot loop.

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
Changes universal Suspense scheduling and use() thenable handling on Lynx/universal paths; behavior shifts for unrecognized status values, but scope is bounded to suspension retries with strong regression tests.

Overview
Fixes a Suspense livelock in the universal renderer (Lynx and other octane/universal targets) when a retry keeps suspending on a thenable that already settled. Retries used to run on that thenable’s next microtask, starving timers and native callbacks so the UI never recovered.

Runtime (universal-core.ts): Adds resumeOnSettle (same idea as the DOM fix in #1400): a WeakSet remembers wakeables whose settlement was already observed. First settlement still retries on a microtask; subsequent retries wait for the next then notification, then setTimeout(0) once. All universal resume paths go through it—suspended attempts, local/root replays, reverse-region owner routing, and DOM-bridge releaseProjection (so held transition work doesn’t re-suspend on the microtask).

use(): trackUniversalThenable only tags thenables with no status; it no longer overwrites foreign statuses (e.g. router-core 'resolved') and treats them as pending, aligned with React’s trackUsedThenable.

Docs and SUSPENSE_DIVERGENCE.md describe the contract; universal-settled-wakeable.test.ts covers boundaries, roots, transitions, hidden Activity, DOM-hosted roots, and custom wakeables.

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

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]>
@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 15:03
trueadm and others added 2 commits September 29, 2026 16:03
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
trueadm marked this pull request as ready for review September 29, 2026 15:12
@trueadm
trueadm merged commit 4180828 into main Sep 29, 2026
48 checks passed
@trueadm
trueadm deleted the claude/quirky-varahamihira-61f669 branch September 29, 2026 15:32
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