Skip to content

fix: write a Vue leaf's deep data without one Rust frame per level - #407

Merged
JuroOravec merged 1 commit into
mainfrom
fix/native-leaf-json-stack
Oct 25, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/native-leaf-json-stack

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

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 stacker guard 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_value writes the first 8 levels with direct calls, as before. Anything deeper 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.

  • 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;
    • a dict's keys are checked and sorted before any of its values.

    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.rs files and the tests.

Is the output the same?

The reviewer compared this writer with a model of main's recursive one, on:

  • 6,000 random values at depths 1 to 230, including the switch at 7 to 10;
  • chains of 0 to 204 levels with every kind of value at the bottom;
  • loops to the outer container and to a middle one, at every depth.

Every decline matched, and every written value matched _script_json(_json_plain(value)) byte for byte.

Tests

Speed

_leaf_values_json runs 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 of main and this branch, in alternating fresh processes:

Leaf data Change by minimum Change by median
3 scalars -2.6% -1.3%
a small dict -0.3% -0.4%
20 rows of 5 fields +2.1% -0.4%
6 nested levels +2.8% +1.5%
  • Conditions: the load average ranged from 1 to 23, so differences of a few percent are within noise.
  • The worst case: about 0.1 µs on a 20-row leaf.
  • Which switch was measured: these runs used the switch at 32 levels. At 8, all four cases still stay on the direct path.

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=0 too, so it is not this writer. Its cause is unknown, and it is worth its own issue.

Checks

  • Rust: cargo clippy -D warnings and cargo fmt --check pass.
  • Python suites: the citry, citry_ui and citry_core suites pass, apart from test_render_body_snapshots.py, which fails only when it shares an xdist worker with Django tests and passes alone (as on main).
  • Generated copies: the protocol copies, the docs playground, the VS Code bundle, the citry-ui assets and scripts/validate.py pass.
  • Review: an independent review at high effort ran twice; its findings are applied.

🤖 Generated with Claude Code

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]>
@JuroOravec
JuroOravec merged commit adec7cd into main Oct 25, 2026
28 of 32 checks passed
@JuroOravec
JuroOravec deleted the fix/native-leaf-json-stack branch October 25, 2026 15:07
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.

The native render path crashes on a small thread when a Vue component's data nests deeply

1 participant