Skip to content

fix: compile and server-render deep Vue templates on any thread - #400

Merged
JuroOravec merged 1 commit into
mainfrom
fix/vue-server-render-stack
Oct 25, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/vue-server-render-stack

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Fixes #387. In a component that uses Vue, deeply nested templates now compile and render on any thread without crashing the process. The server also writes elements nested up to 400 deep inside the host, where before it gave up at about 130.

One part of the issue's goal cannot be met. Chrome's HTML parser nests at most 513 elements, counting <html>, and moves deeper ones out of their parents, so 512 nested elements cannot be written whole on the server. Past 400 elements inside the host, the part becomes a shell, as before.

Prior art

What I measured

  • Levels per element (the issue's unconfirmed claim): confirmed. A plain <div> costs 2 writer levels and 1 reader level, and a v-if or v-for on it adds one of each. At the nesting limits planned in 50 nested <c-if> blocks fail with "too many nested parentheses", and about 1,000 crash the process #272, render code reads about 770 levels and writes about 1,300, so the issue's suggested 1,100 would be too low.
  • Chrome's limit: in headless Chrome, a host inside <body> holds at most 510 nested <div>s; deeper ones are written as siblings. html5ever, which Citry's parse checks use, follows the spec and has no such limit. So raising the caps alone would have produced pages that hydrate with a mismatch in Chrome. WebKit is believed to share Blink's constant; Firefox was not checked.
  • Crashes on small threads:
    • Vue's compiler needs about 9.3 MiB of stack, in a debug build, for 512 nested <div v-for> elements.
    • oxc overflowed a 256 KiB thread reading the render code for 512 nested elements.
    • citry_html_transform's parse comparison and raw-block checks recurse once per level of the parsed tree.
  • Page data: serde_json reads at most 128 nested levels. A page whose data nests deeper, such as 256 c-for= elements (about 261 levels), mounted in the browser.

The change

  • Guards at each level: stack.rs in both crates holds the parser's guard. Each level of these runs through it:
    • the reader (Reader::node, Reader::expr);
    • the writers (Writer::occurrence, Writer::node, StaticWriter::node);
    • eval, fully_supported, normalize_class and style_entries;
    • the parse comparison (Walk::visit_children) and the raw-block checks (allowed, same_tree).
  • Enough stack up front for recursion inside code Citry does not own:
    • a whole compile and a whole read_program: 16 MiB, once per definition;
    • freeing a read program's tree, which ServerRenderProgram's new Drop does: 1 MiB;
    • a render: at least 256 KiB, enough for serde_json's 128 levels, with a 1 MiB segment when the thread has less.
  • Element cap: MAX_ELEMENT_DEPTH = 400. The writer declines an element nested deeper inside the host, so its parent becomes a shell. That leaves about 110 levels for the page's own elements around the host.
  • Level caps: MAX_DEPTH and MAX_READ_DEPTH are both 2,048. They stop much deeper input; the guards, not the caps, protect the stack.
  • Deep pages use the full parse: a page nested more than 256 elements deep uses one full parse of its HTML instead of the token check. On a thread's first deep page, the token check costs time that grows with the square of the depth: the reviewer measured 84 ms at 512 levels in a release build, against 0.8 ms for the full parse. Below 256 elements a warm thread keeps answering from stored token answers. At 400 elements every render pays about 1.4 ms for the second write and the full parse.
  • Deep page data: when serde_json reports its recursion limit and the data nests no deeper than 2,048 levels, the render parses again with unbounded_depth (enabled for this crate only; it adds a method and changes no default) on a 16 MiB stack. Ordinary pages skip the second parse. Deeper data still makes the page mount in the browser.
  • wasm32: stacker cannot see the stack there, so each outermost guarded call takes a block from the heap:
    • a render takes 1 MiB, plus 16 MiB more for data deeper than 128 levels;
    • freeing a program takes 1 MiB;
    • a compile and a read take 16 MiB each.

Tests

  • tests/deep_nesting.rs in citry_vue_compiler (new, its own binary so a crash aborts only it): each case runs on 128 KiB and 384 KiB threads. A render on 384 KiB starts on the thread's own stack, so the guard at each level must work. The cases:
    • 400 nested elements, 256 v-if elements around 144 more, and 256 <template v-for> levels around 256 elements are written whole;
    • 512 nested elements hydrate with one shell;
    • 650 <template v-if> levels around 399 elements pass the writer cap and hydrate with one shell;
    • 512 v-for elements compile;
    • data 520 levels deep is written;
    • data 5,000 levels deep mounts in the browser.
  • tests/deep_nesting.rs in citry_html_transform (new): the parse comparison and the raw-block check on HTML 2,000 elements deep, on the same thread sizes.
  • Against main, and with the guards removed: on main, all 8 citry_vue_compiler tests and both citry_html_transform tests overflow the stack. With only the per-level guards turned into plain calls, 5 crash.
  • test_vue_hydration_regions.py:
    • The issue's shapes are written whole with nothing declined: 400 <div>, 256 <c-if> around 256 <div>, and 256 c-if= and 256 c-for= elements around 144 <div>.
    • 512 nested elements hydrate with one shell, at the 400th element.
    • 400 nested elements render on a thread with a 512 KiB stack. A check confirmed that compile, read and render all run on that thread.
    • On main, the four shapes fail and the thread test crashes the process.
  • render_code_past_the_read_cap_is_left_to_the_browser (renamed): it now uses twice the read cap.

Speed

render_for_hydration runs on every render of the large scenario, so the minimum-CPU gate applies. Release builds of citry-core for main and this branch, 15 alternating fresh processes per side, 30 renders each, 5-minute load average under 3:

Native path main This branch Change
on 62.95 ms 63.49 ms +0.86%
off 65.38 ms 65.29 ms -0.14%

Not in this PR

Checks

  • Rust: cargo test, cargo fmt --check and cargo clippy -D warnings pass for both crates.
  • Python suites: the citry, citry_ui and citry_core suites pass (10,289 tests).
  • Generated copies: the protocol Python copies, the VS Code bundle, the citry-ui assets and scripts/validate.py pass. The docs-playground check fails on main's stale portable_ide.py, which fix: keep the playground's portable IDE rules free of the citry package #389 fixes.

🤖 Generated with Claude Code

Fixes #387. In a component that uses Vue, Citry compiles the template
with Vue's compiler (Vize), reads the render function it emits, and writes
the page's HTML on the server so the browser can adopt it. Each of these
steps calls itself once per nesting level:

- The server renderer capped its depth to stay inside a small stack:
  past 256 writer levels (two per element, so about 130 nested elements)
  or 200 reader levels, it left the part for the browser to build.
- Vue's compiler needed about 9.3 MiB of stack, in a debug build, for 512
  nested `<div v-for>` elements, and `oxc` needed more than a 256 KiB
  thread for the render code of 512 nested elements. Python can run both
  on a thread with 512 KiB (macOS) or 128 KiB (musl Linux), where they
  crashed the process.
- The check that the browser parses the written HTML as written, and the
  checks of a raw HTML block (`citry_html_transform`), walk the parsed
  tree with one call per level.
- serde_json reads at most 128 nested levels, so a page whose data nests
  deeper (about two levels per `<c-for>`) mounted in the browser.

Now:

- `citry_vue_compiler/src/stack.rs` and `citry_html_transform/src/stack.rs`
  hold the same `stacker` guard as the template parser. Each level of the
  reader, the writers, `eval`, `fully_supported`, the class and style
  normalizers, the parse comparison and the raw-block checks runs through
  it. Recursion inside code Citry does not own gets enough stack up front:
  a whole compile and a whole `read_program` (16 MiB), and freeing a read
  program's tree, which `ServerRenderProgram`'s new `Drop` does (1 MiB).
- The writer declines an element nested more than 400 elements deep
  inside the host into a shell. Chrome's parser nests at most 513
  elements, counting `<html>` (a host in `<body>` holds at most 510 nested
  `<div>`s, measured in headless Chrome), and writes deeper ones as
  siblings. html5ever, which the checks use, follows the HTML standard,
  which sets no such limit. The 400 leaves about 110 levels for the page's
  elements around the host.
- The writer's and the reader's level caps are 2,048. A template at the
  nesting limits planned in #272 (512 tags, 256 of them control flow)
  reads about 770 levels and writes about 1,300 (measured).
- A page nested more than 256 elements deep uses one full parse of its
  HTML instead of the token check, whose first run on a thread costs time
  that grows with the square of the depth (84 ms at 512 levels against
  0.8 ms for the full parse, measured in a release build). Below that
  depth a warm thread keeps answering from stored token answers.
- `render_for_hydration` runs with at least 256 KiB of stack, enough for
  serde_json's 128 levels. When the first parse reports that limit and the
  data nests no deeper than 2,048 levels, it parses again with
  `unbounded_depth` on a 16 MiB stack, so ordinary pages skip the second
  parse. Deeper data still makes the page mount in the browser.

Tests: `tests/deep_nesting.rs` in both crates runs each step on 128 KiB
and 384 KiB threads (384 KiB starts a render on the thread's own stack,
so the guard at each level must work). On `main`, all 8
`citry_vue_compiler` tests and both `citry_html_transform` tests overflow
the stack; with the per-level guards turned into plain calls, 5 crash.
The Python tests check that the issue's shapes, at 400 elements, are
written whole on the server, that 512 nested elements hydrate with one
shell at the 400th element, and that a deep page renders on a 512 KiB
thread; the parametrized ones and the thread test fail on `main`.

Not in this change:

- The native render path's leaf JSON writer
  (`citry_core_py/src/native_render/leaf_json.rs`) also calls itself once
  per level of a leaf's data, up to 200, and reads Python objects, so a
  stack guard cannot cover it.
- Compiling about 1,200 nested `<div v-for>` elements (debug build) or
  2,000 (release) still overflows the 16 MiB compile stack and crashes the
  process, as on `main`. Templates that deep stay reachable until #272's
  nesting limit lands in the parser.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vue server HTML leaves content to the browser past 129 nested elements

1 participant