Repository navigation
fix: write a Vue leaf's deep data without one Rust frame per level - #407
Merged
Merged
Conversation
Fixes #398. On the native render path, Rust writes the JSON of a Vue component's leaf data (`_leaf_values_json` in `citry_core_py/src/native_render/leaf_json.rs`). Its `write_value` called itself once per nested dict, list or tuple, up to its limit of 200 levels. A thread gets the platform's default stack unless Python sets one: 128 KiB on musl Linux, also for Python's own threads, and 512 KiB on macOS for a thread started outside Python; `threading.stack_size()` can make any Python thread as small. There, deep data crashed the process with no Python traceback. Each level reads Python objects, so the `stacker` guard the other crates use cannot help: CPython 3.14 and later check the stack pointer against the thread's own stack, and a heap segment is outside it. `write_value` still writes the first 8 levels with direct calls, which keep shallow data as fast as before; one level takes about 3.3 KiB of stack in a debug build, so these take about 26 KiB. A container deeper than that goes to `write_with_stack`, which keeps each container it has opened but not finished on a list, so the stack stays the same at any depth. Both walks share the same checks, in the same order: a scalar is written as it comes, a container's depth and cycle checks run before it opens, and a dict's keys are checked and sorted before any of its values is written. So the first value refused, the decline it names, and the JSON written are unchanged. The 200-level limit stays, as a bound on the cycle check; its comment no longer says Python's conversion raises `RecursionError`, which #391 fixed. Tests: - Data one level short of the limit is written on 128 KiB and 512 KiB Python threads and compared with Python's JSON; on `main` it crashes the process. - Each decline reason, a loop, the first of two refused values, and the JSON written are checked on each side of the switch, and a loop that starts and closes past the switch is refused. - #400's thread test (`test_a_deep_template_renders_on_a_thread_with_a_small_stack`) now renders the `c-for attributes` template, whose leaf data nests about 512 containers deep; Rust walks 200 levels of it before leaving it to Python, so the test covers this writer too, as #398 asks. #400 described the small stacks as the default for threads other than the main one; Python's own threads get 16 MiB on macOS. That wording is corrected in both `stack.rs` files and the test. Speed: `_leaf_values_json` timed directly with release builds of `main` and this branch, in alternating fresh processes, while the machine's load average ranged from 1 to 23 (negative means faster): three scalars -2.6%, a small dict -0.3%, 20 rows of 5 fields +2.1% by minimum (-0.4% by median), and 6 nested levels +2.8% (+1.5%). Those measurements used a switch at 32 levels; at 8, all four cases still stay on the direct path. The large benchmark scenario never calls this function, so the usual whole-render gate cannot show it. 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 #398. On the native render path, Rust writes the JSON of a Vue component's leaf data (
_leaf_values_json). Its writer called itself once per nested container, up to 200 levels. On a thread with a small stack, deep data crashed the process with no Python traceback.Why a stack guard cannot fix it
The writer reads Python objects at every level. CPython 3.14 and later check the stack pointer against the thread's own stack, so the
stackerguard that #400 uses elsewhere cannot move this work onto a heap segment.Small stacks are common: a thread gets the platform's default unless Python sets one. That is 128 KiB on musl Linux, also for Python's own threads, and 512 KiB on macOS for a thread started outside Python.
threading.stack_size()can make any Python thread as small.The change
Two walks:
write_valuewrites the first 8 levels with direct calls, as before. Anything deeper goes towrite_with_stack, which keeps each container it has opened but not finished on a list, so the stack stays the same at any depth.The same checks in the same order:
So the JSON written, the first value refused, and the decline it names are all unchanged.
The 200-level limit stays. Its only job now is to keep the cycle check short.
Stack-size wording: fix: compile and server-render deep Vue templates on any thread #400 described the small stacks as the default for every thread other than the main one, which is wrong: Python's own threads get 16 MiB on macOS. This PR corrects that in both
stack.rsfiles and the tests.Is the output the same?
The reviewer compared this writer with a model of
main's recursive one, on:Every decline matched, and every written value matched
_script_json(_json_plain(value))byte for byte.Tests
mainit crashes the process (exit 132).c-for attributestemplate, as The native render path crashes on a small thread when a Vue component's data nests deeply #398 asks. Its leaf data nests about 512 containers deep, and Rust walks 200 levels of it before leaving it to Python.Speed
_leaf_values_jsonruns on every render of an ordinary Vue component on the native path, but the large benchmark scenario never calls it (0 calls counted). So I timed it directly, with release builds ofmainand this branch, in alternating fresh processes:Known limit
In a debug build, one level of the direct walk takes about 3.3 KiB of stack. On a 128 KiB thread, a 200-deep write still crashes if it starts within about 11 C-level Python frames of the point where Python itself would raise
RecursionError. Lowering the switch to about 5 levels would close that gap, at some cost for data 5 to 8 levels deep. I kept 8, the depth the timing above covers. Release builds have smaller frames; I did not measure them.The reviewer also found a separate crash. The same deep-template render on a 512 KiB thread crashes with no traceback when about 620 to 650 C-level frames are already in use, with
CITRY_NATIVE_RENDER=0too, so it is not this writer. Its cause is unknown, and it is worth its own issue.Checks
cargo clippy -D warningsandcargo fmt --checkpass.test_render_body_snapshots.py, which fails only when it shares an xdist worker with Django tests and passes alone (as onmain).scripts/validate.pypass.🤖 Generated with Claude Code