Skip to content

fix(ssr): let prerender outlast a reader that rethrows a settled thenable - #1409

Merged
trueadm merged 2 commits into
mainfrom
claude/trusting-robinson-309a17
Sep 29, 2026
Merged

trueadm merged 2 commits into
mainfrom
claude/trusting-robinson-309a17

Conversation

@trueadm

@trueadm trueadm commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

prerender now finishes when a resource reader keeps rethrowing a thenable that has already settled while its own state waits for a later task, such as a timer or an I/O callback. Before this change, buffered SSR retried on microtasks. It spent all 50 passes before that task could run, then failed with a message that blamed use().

  • Stall detection. A buffered wave stalls when every registration is a resource-thrown thenable ('|throw#') that this render has already seen settle. runBuffered checks this before settling, in isStalledWave.
  • Pass budget and pacing. Stalled waves don't spend the 50-pass budget. After the thenables notify again, the retry waits on a timer that starts at 1ms and doubles up to 100ms, so a reader that never recovers costs about ten renders a second.
  • Deadline. The stall fails with new error 332 once it lasts timeoutMs (default 10s; 0 disables it).
    • When a stalled wave has fewer registrations than any earlier wave in the same stall, a reader has finished, and the rest get a fresh deadline.
    • Width only shrinks, so this happens at most once per reader, and a reader cycling between settled thenables stays bounded.
  • Error messages. When a pass limit is reached on a wave made only of thrown thenables, buffered (333) and streamed-boundary (334) errors now name thrown thenables instead of use().

Why this design

React is not the reference here. In React 19.2.7, Fizz's pingTask schedules retries with setImmediate only once a streaming request is open. For all of prerender, and during a stream's first task, it pings on a microtask. In a probe, React's prerender re-rendered 200,000 times and never let a 0ms timer fire. This change deliberately does better than React; the docs record the divergence.

Identity only. My first version also counted a native promise that was already settled when thrown as stalled. That covered a reader throwing a new Promise.resolve() on every attempt. But it made every level of a cached thrown-resource waterfall look stalled too, and a micro-benchmark went from 138µs to 8.8ms per render. Identity cannot misread that pattern, and it matches the client rule in #1400. A reader that throws a new settled promise on every attempt still hits the pass limit, now with error 333; this is documented as a limit.

Streaming is not changed here. It already yields one macrotask per wave, but each wave still counts toward the 50 consecutive passes, so a 20ms lag fails there. A follow-up session is working on streamed stalls on top of this diff.

Changes

  • packages/octane/src/runtime.server.ts: isThrownKey, isThrownWave, and isStalledWave; settleSuspended records settled thrown thenables in resolved.settledThrows. Also the stall accounting and backoff in runBuffered, and error-code selection for 47/333 and 48/334.
  • Error codes 332–334 in packages/octane/error-codes/codes.json, plus the regenerated error-codes.server.generated.ts and CLI data.
  • packages/octane/audit/SUSPENSE_DIVERGENCE.md: section 9 describes the server behavior, React's behavior, and the limits. Section 13's "SSR is unchanged" limit now points to section 9.
  • docs/differences-from-react.md and docs/ssr.md (timeoutMs), plus a changeset.

Validation

  • Targeted tests: 19 new cases beside the resource-thrown tests in packages/octane/tests/ssr-suspense.test.ts, in both the octane and octane-prod projects.
    • The readers rethrow a resolved or rejected promise, a custom thenable, or a promise that was pending first, with and without a boundary.
    • Further cases cover the pass budget during a stall, the deadline and bounded retries, per-reader deadline restarts, a cycling reader, and abort.
    • A cached waterfall of already-resolved thrown resources must render before any task runs.
    • Both new pass-limit messages are checked, with a use() control that keeps the old message.
  • Mutation checks: each test was seen failing for the regression it guards. I broke the fix nine ways: no pacing, no identity record, stalls spending the budget, no backoff, treating every thrown wave as stalled, restarting the deadline too often, no deadline restart, and no 333 or 334 selection. Each break failed at least one test. The readers open themselves after 2,000 checks, so a microtask livelock fails fast instead of hanging Vitest.
  • Before merging main, the full octane, octane-prod, and cli projects gave 24,796 of 24,797 passed. The one failure was a hydration/independent-parent.test.ts load flake; it uses synchronous renderToString, never reaches this code, and passes when re-run alone.
  • After merging main, the SSR, streaming, static, hydration, suspense, actions, react-compat SSR, and production-error-bundle suites gave 3,563 of 3,564 passed at load average 35–47. The failure was the same flake, which passed 2 of 2 alone.
  • pnpm error-codes:check, pnpm sync, and pnpm format:files:check on all changed files.
  • pnpm typecheck:files on the changed source is clean. The test file's four existing errors (a .tsrx import and older replayThenable fixtures) are outside this diff.
  • Repo-wide pnpm format:check, pnpm typecheck, and pnpm test: not run locally; CI covers them.

Performance (ssr-throughput):

  • Frozen baseline and candidate bundles ran A/B, in one process, alternating 250ms blocks. The machine was shared and heavily loaded, and separate processes showed ±25% noise even on configs that never reach this code.
  • waterfall-d1/d2/d4, nested-d4, deopt-fast, and control-flow were within −1.0% to +0.4%, with no consistent winner and byte-identical bodies.
  • A thrown-resource micro-benchmark (waterfalls and 8 parallel boundaries, pending or already resolved, plus a use() control) moved between −1.5% and +2.6% across processes, so there's no consistent change.
  • The minified fixture SSR bundle grows by 1,205 bytes raw (332 bytes gzipped), including the three new development messages. The client runtime is unchanged.

Risk / follow-ups

  • Shared wakeable edge case. A custom wakeable that is reused and rethrown for several genuinely pending loads is treated as stalled after its first settlement. So the stall deadline, not the pass count, bounds it. I don't know of a reader that works this way.
  • Streamed stalls (fix(octane): pace Suspense retries on an already-settled wakeable #1400-style pacing for renderToPipeableStream and renderToReadableStream) are in progress in a separate change built on this one.
  • Error 32 ("a use(thenable) did not settle") still names use() when a resource-thrown thenable never settles. I left it unchanged.

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 core buffered SSR suspense retry logic and failure modes; behavior shifts for resource readers that lag behind settled promises, though scope is server-only with extensive new tests.

Overview
Buffered prerender no longer burns the 50-pass suspense budget when a resource reader keeps throwing the same already-settled thenable until a timer or I/O callback updates its state. The server tracks settled resource throws in resolved.settledThrows, treats an all-|throw# wave as stalled via isStalledWave, and after settle waits on a 1ms→100ms backoff timer instead of microtask spins. Stalled retries do not increment the pass counter; unrecoverable stalls fail with new error 332 after timeoutMs (also documented for prerender).

When pass limits hit on waves made only of thrown thenables, buffered (333) and streaming-boundary (334) errors now blame resource throws outside use() instead of generic use() messages. Docs/changeset record the React divergence; ssr-suspense.test.ts adds coverage for pacing, deadlines, waterfalls, abort, and the new diagnostics.

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

trueadm and others added 2 commits September 29, 2026 17:54
…able

A resource reader can keep throwing a thenable that has already settled while
its own state waits for a later task, such as a timer or an I/O callback.
Buffered SSR retried on microtasks, so it spent all 50 passes before that task
could run and failed with a message that blamed use().

runBuffered now recognizes a stalled wave: every registration is a
resource-thrown thenable this render has already seen settle. Stalled waves do
not spend the pass budget. After the thenables notify again, the retry waits on
a timer that doubles from 1ms to 100ms, and the stall fails with a new error
(332) once it lasts timeoutMs. A narrower stalled wave means a reader finished,
so the rest get a fresh deadline. Width only shrinks, so a reader that cycles
between settled thenables stays bounded.

Stalls are recognized by identity only. Probing whether a thenable was settled
when thrown also caught a new Promise.resolve() per attempt, but it made every
level of a cached thrown-resource waterfall look stalled, slowing that case
from 138us to 8.8ms per render.

When a pass limit is reached on a wave made only of thrown thenables, buffered
(333) and streamed boundary (334) errors now name thrown thenables instead of
use().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…son-309a17

Keep both retry-pacing bullets in differences-from-react.md, client (#1400)
first. Point section 13's "SSR is unchanged" limit at the buffered server
rule in section 9.

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 17:02
@trueadm
trueadm marked this pull request as ready for review September 29, 2026 17:31
@trueadm
trueadm merged commit 32c1bf4 into main Sep 29, 2026
55 of 58 checks passed
@trueadm
trueadm deleted the claude/trusting-robinson-309a17 branch September 29, 2026 18:02
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