Repository navigation
fix(native-render): leave templates nested past a depth limit to Python - #499
Merged
Merged
Conversation
The native render path builds a plan for a simple='vue' template, then evaluates it and writes each row's data, with code that calls itself once per nesting level. That work reads Python objects, so it cannot move to a stack taken from the heap. A template nested deeply enough crashed the process on a thread with a small stack, such as musl's 128 KiB default. Rust now refuses a template past two limits: one for the c-if and c-for bodies around an operation plus the conditional expressions inside it, and one for the paths it reads. The refusal is a ValueError starting with "too_deep:", which Python caches as "no plan". The first render that meets it logs one debug message naming the component, and every row then renders through Python's evaluator with the same output. A debug build's frames are several times larger, so each build profile has its own limits (64 and 64 in a release build, 8 and 16 in a debug build), and _rust.NATIVE_RENDER_MAX_BODY_DEPTH and _rust.NATIVE_RENDER_MAX_READ_DEPTH report the active ones. Tests check the refusal at each limit, the output on both sides, the log message, and rendering at and past the limits on a fresh 128 KiB thread in a separate interpreter. CI runs those in the Linux 3.14 cell only. The note in docs/codebase.md now says that uv sync builds a release extension. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 #422 with option A of the design note: Rust leaves a
simple='vue'template nested past fixed depth limits to Python's evaluator. Options B and C are parked in #496.What happens now
The native render path builds a plan for a
simple='vue'template, then evaluates it and writes each row's data, with code that calls itself once per nesting level. The PyO3 code that reads Python's description of the plan, and copies and rebuilds a row's values, does the same. That work reads Python objects, so it cannot move to a stack taken from the heap. A template nested deeply enough crashed the process on a thread with a small stack.Rust now refuses a template past either of two limits:
c-ifbranch orc-forbody around an operation, one for the operation's expression, and one for each conditional expression inside it.{{ n }}inside six nested loops is level 8.The refusal is a
ValueErrorstarting withtoo_deep:.native_row_plancaches it as "no plan", and the first render that meets it logs one debug message on thecitrylogger that names the component and says how the levels count. Every row then renders through Python's evaluator, with the same output.uv sync, CI, the published wheels)maturin develop)The two builds differ because debug stack frames are several times larger.
citry_core._rust.NATIVE_RENDER_MAX_BODY_DEPTHandNATIVE_RENDER_MAX_READ_DEPTHreport the active limits, besideBUILD_PROFILE, so tests read them instead of copying them.How the limits were chosen
I rendered each shape on a fresh thread, with its row plan built on that thread for the first time, which is the deepest the work goes. The shapes were nested loops, nested
c-ifs, nested conditional expressions and one deep read path. A throwaway script bisects for the first depth that crashes. I ran it on this branch's final code with the limits lifted, on macOS arm64 and Python 3.14:c-ifThe reviewer also tried shapes that mix loops,
c-ifchains withc-elifandc-else, a rootc-bind, conditional attributes and a deep path inside loops, on a debug build. None used more stack per level than plain loops orc-if.I tried splitting
lower_opsandBuilder::opsinto one function per case:So both stay as single functions. The error is built in a
#[cold]function, so a level does not keep space for it. With that, a release build reaches 139 levels, against main's 142.Decisions
Juro chose option A. The open questions were settled as follows:
cfg!(debug_assertions). The first plan was to take the limits from debug builds, "so CI keeps them current". It turned out CI tests a release build:uv syncbuilds through maturin's PEP 517 backend, which uses Cargo'sreleaseprofile. So each build got its own limit. This PR also corrects the sentence indocs/codebase.mdthat saiduv syncbuilds a debug extension.PyReadNodetypes. They stay derived. The limits bound how deep they go, and they hold far fewer locals than the walks that were measured, but their stack per level was not measured on its own.CITRY_NATIVE_DEPTH_PROBES=1. A local run always runs them, so amaturin developrun checks the debug limits too.Prior art
These are what I read; line numbers are from
73e94cb77:Plan::new,Builder::ops,Builder::exprandplan_read_nodeincrates/citry_native_render/src/plan.rs.Evaluator::ops,exprandtruth, andwrite_record/write_entry, ineval.rs. These are the pure-Rust walks; none counted depth.lower_ops,lower_expr,lower_read_node,PyReadNode::new,read_valueandsnapshot_valueincrates/citry_core_py/src/native_render/rows.rs. These are the walks that touch Python.crates/citry_core_py/src/native_render/mod.rs:13-19, which records why level A has no stack guard.too_deepdecline at depth 200 (leaf_json.rs), and how its test reads the limit from Rust. This PR follows both patterns.native_row_planincitry/_vue/leaf_program.py:2527-2558, which caches aValueErroras "no plan" and lets any other error propagate.crates/citry_template_parser/src/stack.rsandpackages/py/citry_core/tests/test_stack_guards.py. These are the guarded walks and the existing subprocess stack tests.Alternatives
What would show this is wrong
Error modes
_NativeRunis skipped. The output is the same, with one debug log line per template.Drop,CloneandPartialEqof the plan and value types call themselves once per level too. The limits bound them; their stack per level was not measured on its own.Cross-binding audit (Mechanism 4)
citry_native_rendergainsMAX_BODY_DEPTH,MAX_READ_DEPTH,too_deep,BODIESandREAD_PATHS, plustests/depth_limits.rs.citry_core_py/src/native_render/rows.rscounts depth while it reads the description.citry_core_py/src/lib.rsadds two read-only module constants,NATIVE_RENDER_MAX_BODY_DEPTHandNATIVE_RENDER_MAX_READ_DEPTH. This is the only change to what Python sees;_RowPlan's arguments and behavior within the limits are unchanged._rust.pyi: declares both constants.citry/_vue/leaf_program.pyrecords a depth refusal, andcitry/component_render.pylogs it once with the component's name.src/lang/*.rs: not involved. The native render path has no per-language implementation.crates/citry_native_render/tests/depth_limits.rs,packages/py/citry/tests/test_native_render_depth.py, andpackages/py/citry_core/tests/test_build_profile.py.Tests
Rust: each shape (loops,
c-if, conditionals, path, loops with a path) plans at its limit and is refused one past it, and a plan at the limit writes its row. These pass on both the debug and the release profile.Python, every cell:
_RowPlanaccepts each shape at the limit and refuses it one past withtoo_deep, including loops with a subscripted path inside, where loop items and subscripts both count toward the read limit.render_both).Python, one Linux cell (or any local run): these render on a fresh 128 KiB thread in a separate interpreter, with the plan built on that thread:
c-ifand a read path, each at the limit (two rows read in Rust) and one past it (none);Both outputs rebuild the Rust-read row with
NativeRow.snapshot(), and the thread collects garbage afterwards. Each case takes about 0.2 to 0.5 s.Run locally:
cargo test -p citry_native_render, in both debug and release;cargo clippy --no-deps -p citry_native_render -p citry_core_py --all-targets -- -D warnings;cargo fmt --check;--python-render-path;ruff, andmypy(also with--platform linux) onleaf_program.py.🤖 Generated with Claude Code