Skip to content

fix(octane): recover when server HTML loses a hydration range's closing marker - #1753

Merged
trueadm merged 3 commits into
mainfrom
fix/unclosed-hydration-range
Oct 5, 2026
Merged

trueadm merged 3 commits into
mainfrom
fix/unclosed-hydration-range

Conversation

@trueadm

@trueadm trueadm commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Hydration crashed when server HTML lost a range's closing <!--]-->. Some HTML minifiers and proxies strip comments, so this can happen in production. findMatchingClose assumed every range closes inside its parent. It walked off the end and threw TypeError: Cannot read properties of null (reading 'nodeType'). The root reported that through onUncaughtError and emptied the container. When the missing close was a 'use dom bindings' entry view's own root pair, hydrateRoot threw synchronously instead. The problem was found while working on #1743 (PR #1751), but it reproduces without early bindings: a plain component between static siblings is enough.

Now the walk throws UnclosedHydrationRange when the parent ends first. The nearest owner whose own range is intact renders on the client instead of adopting, and reports one recoverable error (#338):

  • A Suspense arm. The arm's own close was matched before its attempt, so the damaged range lies inside it. The boundary discards its server arm and mounts the body on the client. This also covers an arm whose hydration suspends and finishes after the root commits. Before this change that case crashed after commit, with no root attempt left to recover it.
  • A <Hydrate> boundary. It rolls back and mounts the island live. Content outside the boundary keeps its server nodes.
  • Otherwise the root. It rolls back its attempt and calls root.render.

Application @catch arms and ErrorBoundarys never receive the error.

Why no local repair

I first tried to repair the DOM by closing the range at the end of its parent, the way an HTML parser closes an element whose end tag is missing. That stopped the crash, but the final DOM was wrong. The parent template adopts its <section> wholesale, so the static <i> after the component is never claimed on its own. The extended range then discarded <i> as stale tail content.

No guessed extent is safe. One that ends too late discards static template content after the range. One that ends too early leaves server nodes that no slot claims. So recovery goes to the nearest owner whose own range is trustworthy.

Design notes

  • UnclosedHydrationRange extends NativeAdoptionMiss. Every boundary already passes that error through via isAdoptionControl, and the root and <Hydrate> owners already recover it by rolling back and rendering live. Subclassing reuses that path unchanged. Adding a separate class to isAdoptionControl would have put it in every client bundle: cli-spa-starter grew 74 raw bytes when I tried it.
  • The Suspense recovery journals the removed server arm (journalRootChildren plus the outer capability's save). If a later root-level suspension rolls the attempt back, the server arm is restored and the attempt that commits reports exactly once. A probe confirmed this.
  • The binding-view root pair's close() now runs inside the root attempt's try, so a missing root close recovers like any other range instead of escaping hydrateRoot.
  • I checked docs/ssr.md and docs/differences-from-react.md first: neither states a contract for malformed markers. docs/ssr.md now documents this behavior. It matches React's model, where a hydration error inside Suspense client-renders that boundary and otherwise the root.

Validation

  • pnpm format:check: not run repo-wide. pnpm format:files:check passes on every changed file.
  • pnpm typecheck: not run repo-wide. pnpm typecheck:files passes on runtime.ts, the test, and the fixture.
  • pnpm test: left to CI; local runs are limited while many sessions share this machine.
  • targeted tests:
    • packages/octane/tests/hydration/unclosed-range.test.ts (new): 50/50 in octane and octane-prod, on the merge with main bda419a.
      • Shapes covered: a plain component, a 'use dom bindings' component, a component in a @try body, a 'use dom bindings' entry view's root pair, and a Suspense arm whose hydration resumes after the root commits.
      • Each runs under three builds: dev compile, prod compile, and prod compile with the prod runtime.
      • It also covers a <Hydrate> island.
      • Each case asserts no uncaught error, exactly one recoverable error (#338 minified under the prod runtime), and final markup equal to the undamaged server render. It also checks that the client-rendered tree is live: clicks fire and updates apply in place.
      • An intact-marker control shows the server button keeps its identity and nothing is reported.
      • On unpatched runtime.ts all 16 damaged cases fail (TypeError, a synchronous throw, or a timeout for the island) and the 9 controls pass. With only the Suspense branch removed, the 3 Suspense cases fail.
    • --project octane: suspense-hydrate, initial-suspense-hydrate, early-control-handoff, range-boundary, deferred-hydration, suspended-clone-mismatch (108/108); scalar-dom-bindings, behavior-root (307/307). 73 of the latter go through the moved binding-view root block (checked by making that block throw).
    • Exploratory probes, not committed: @if arms, nested arms, @switch, keyed and unkeyed @for lists, ErrorBoundary, a @pending arm in the same parent, and a root-level suspension before the damaged range. All recover with one report and correct DOM.
    • pnpm error-codes:check passes. pnpm sync was run and its CLI data output is committed.

Bundle size. Measured with run-minimal.mjs on the merge with main bda419a:

scenario main this PR Δ
hydrate-root raw / gzip 173,813 / 55,160 174,125 / 55,244 +312 / +84
deferred-hydration raw / gzip 237,321 / 75,020 237,634 / 75,127 +313 / +107
cli-spa-starter, root-static, suspense-transition — — 0

The growth is hydration-only:

  • About 128 bytes are the subclass and the throw.
  • About 184 bytes are the Suspense-arm recovery, which is what keeps a hydration that finishes after commit from crashing.

Both scenarios have about 32 bytes of headroom, so the raise goes in the separate budget-only PR #1754 per CONTRIBUTING "Size budgets". That PR merges first, then main merges into this one.

Risk / follow-ups

  • A missing open marker is still handled the old way: the component goes unframed, adopts its content, and leaves the stray <!--]--> in the DOM without reporting. That cannot crash, but a stray close in a parent that holds an enclosing range could mis-terminate that range's walk. I left it out of scope here.
  • A NativeAdoptionMiss thrown inside a Suspense arm's speculative attempt still goes to the root, as before. Only the unclosed-range subclass is recovered at the arm.

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 hydration cursor matching and multi-boundary recovery paths in runtime.ts, but behavior is narrowly scoped to malformed marker HTML and reuses existing NativeAdoptionMiss rollback semantics with new regression tests.

Overview
Hydration no longer hard-fails when a server range is missing its <!--]--> closing comment (e.g. after comment-stripping minifiers or proxies). Instead of walking past the parent and throwing Cannot read properties of null (reading 'nodeType'), findMatchingClose throws UnclosedHydrationRange, which is treated like other adoption misses.

Recovery re-renders on the client at the smallest trustworthy owner: the enclosing Suspense arm (including arms that finish hydrating after the root commits), a <Hydrate> island, or the root—without surfacing the error to app @catch / ErrorBoundary. The damaged server slice is removed, onRecoverableError fires once with new client error #338, and the rest of the page can keep adopted server DOM where boundaries allow.

hydrateRoot now matches binding-view root range closers inside the hydration attempt so a missing root close recovers instead of escaping synchronously. Docs (docs/ssr.md) and error catalog entries document the behavior; hydration tests cover plain/bindings/@try/Suspense/Hydrate scenarios.

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

trueadm and others added 3 commits October 5, 2026 11:55
findMatchingClose assumed every server range closes in its parent and
dereferenced null when a minifier or proxy had stripped the close. It now
throws UnclosedHydrationRange, a native adoption miss: application
boundaries pass it through and the root or a Hydrate boundary renders on
the client and reports it as recoverable. A Suspense arm whose own range
is intact renders on the client itself, so a hydration that finishes after
the root commits recovers too. The binding-view root pair is matched
inside the root attempt so its missing close recovers the same way.

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 Oct 5, 2026
@trueadm
trueadm marked this pull request as ready for review October 5, 2026 10:59
@trueadm
trueadm merged commit ace4737 into main Oct 5, 2026
27 of 47 checks passed
@trueadm
trueadm deleted the fix/unclosed-hydration-range branch October 5, 2026 11:00
trueadm added a commit that referenced this pull request Oct 5, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is ON, but it could not run because the branch was deleted or merged before autofix could start.

Reviewed by Cursor Bugbot for commit 51a4cef. Configure here.

journalRootChildren(parent);
previousHydration?.save(parent);
removeRange(getNextSibling(state.start), state.end);
initial.consume?.();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recovery drops streamed seeds early

Medium Severity

The new Suspense unclosed-range path calls initial.consume?.() and deletes initialSuspenseHydrations immediately. The catch-arm path defers that work through finishInitialSuspenseHydration until the attempt commits. A later rollback can restore the server arm's DOM, but the streamed payload is already gone from both initialStreamedPayloads and window.$OCTS, so the retry hydrates that arm without seeds.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 51a4cef. Configure here.

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