Repository navigation
fix: compile and server-render deep Vue templates on any thread - #400
Merged
Merged
Conversation
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]>
This was referenced Oct 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
crates/citry_template_parser/src/stack.rs(with_enough_stack,stacker::maybe_growwith a 100 KiB red zone and 1 MiB segments, added by 50 nested<c-if>blocks fail with "too many nested parentheses", and about 1,000 crash the process #272's PR C1) and itstests/deep_nesting.rs. ItsDrop for Template(ast.rs:1098) frees one level per guarded call.MAX_DEPTH = 256andMAX_READ_DEPTH = 200inserver_render.rs. Its only depth test used 2,000 interpolations.stackerincitry_vue_compilerorcitry_html_transform.stackeris pinned in the rootCargo.toml(0.1.24).docs/design/vue_ssr_selected_tree_plan.md. 50 nested<c-if>blocks fail with "too many nested parentheses", and about 1,000 crash the process #272's C2, which adds a nesting section to the syntax docs and enforces the nesting limits, has not landed, so there is no user doc to change.What I measured
<div>costs 2 writer levels and 1 reader level, and av-iforv-foron 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.<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.<div v-for>elements.oxcoverflowed 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.c-for=elements (about 261 levels), mounted in the browser.The change
stack.rsin both crates holds the parser's guard. Each level of these runs through it:Reader::node,Reader::expr);Writer::occurrence,Writer::node,StaticWriter::node);eval,fully_supported,normalize_classandstyle_entries;Walk::visit_children) and the raw-block checks (allowed,same_tree).read_program: 16 MiB, once per definition;ServerRenderProgram's newDropdoes: 1 MiB;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.MAX_DEPTHandMAX_READ_DEPTHare both 2,048. They stop much deeper input; the guards, not the caps, protect the stack.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.stackercannot see the stack there, so each outermost guarded call takes a block from the heap:Tests
tests/deep_nesting.rsincitry_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:v-ifelements around 144 more, and 256<template v-for>levels around 256 elements are written whole;<template v-if>levels around 399 elements pass the writer cap and hydrate with one shell;v-forelements compile;tests/deep_nesting.rsincitry_html_transform(new): the parse comparison and the raw-block check on HTML 2,000 elements deep, on the same thread sizes.main, and with the guards removed: onmain, all 8citry_vue_compilertests and bothcitry_html_transformtests overflow the stack. With only the per-level guards turned into plain calls, 5 crash.test_vue_hydration_regions.py:<div>, 256<c-if>around 256<div>, and 256c-if=and 256c-for=elements around 144<div>.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_hydrationruns on every render of the large scenario, so the minimum-CPU gate applies. Release builds of citry-core formainand this branch, 15 alternating fresh processes per side, 30 renders each, 5-minute load average under 3:mainNot in this PR
crates/citry_core_py/src/native_render/leaf_json.rs) also calls itself once per level of a leaf's data, up to 200, and it reads Python objects, so the stack guard cannot cover it. That is The native render path crashes on a small thread when a Vue component's data nests deeply #398. The Python thread test therefore uses nested elements, whose data is flat.v-forelements still crashes a 128 KiB thread, as onmain. 50 nested<c-if>blocks fail with "too many nested parentheses", and about 1,000 crash the process #272's C2 will reject such templates in the parser.Checks
cargo test,cargo fmt --checkandcargo clippy -D warningspass for both crates.scripts/validate.pypass. The docs-playground check fails onmain's staleportable_ide.py, which fix: keep the playground's portable IDE rules free of the citry package #389 fixes.🤖 Generated with Claude Code