Repository navigation
Description
Background
The native render path builds, evaluates and writes a simple='vue' row's plan with Rust code that calls itself once per nesting level. That work reads Python objects, so it cannot move to a stack taken from the heap. The fix for #422 stops the crash this caused on threads with small stacks, using option A of the design note:
- Rust refuses a template nested past fixed limits, and Python's evaluator renders it instead.
- Each build profile has its own limits. A release build allows 64 levels of
c-if/c-forbodies and conditional expressions, and 64 levels of read paths. A debug build allows 8 and 16.
The cost is that a template past the limit renders more slowly everywhere, even on a thread with plenty of stack. This issue keeps the two options that would remove that cost, for after 0.7.0.
Option B: check the remaining stack at run time
Above option A's limit, Rust would check how much of the thread's stack is left before each level, using stacker::remaining_stack(). It would decline only when too little is left. Templates past the limit would then stay native on threads that have room. Before B is safe, these questions need answers:
- musl. On a musl main thread (Alpine, gunicorn sync workers, uvicorn's event loop), the reading reports only the part of the stack mapped so far. So it is far too low, and every row would decline silently. Should Citry take its own reading there, or treat the reading as unknown and keep option A's limit? CI has no musl cell to catch a mistake. The existing stack guards probably misread there in the same way.
- The reserve. This is the stack Rust must leave free: the most it uses between two checks, plus CPython's soft limit (2 ×
_PyOS_STACK_MARGIN_BYTES), the guard page, room for a panic, and a margin. Should a CI check keep it current as frames change, or is a one-off measurement enough? - A plan build that runs short. Should it retry on the next row, or be recorded so it is not tried again? Catching
RecursionErrorfrom_RowPlanwould widen the errors Citry swallows, whichnative_row_planavoids on purpose. - An unknown reading, such as glibc's main thread without
/proc. The proposal is to use option A's limit.
Option C: split the pure-Rust work from the Python reads
This option runs the pure-Rust work (evaluation, the JSON writer, and the drops) under a stack guard: with_enough_stack, which moves work to a 1 MiB stack taken from the heap when the thread's own stack runs low. It also rewrites the four recursive functions that read Python objects as loops that keep their own list of levels: reading the plan description, copying a row's values, rebuilding them in NativeRow.snapshot(), and building the read nodes. The loops must read in exactly the current order. Deep templates would stay native on small threads. Stack already in use when the render starts is still not covered, read depth still needs a limit, and the guards inherit the musl reading problem from option B. The note estimates about 400 to 600 lines, at medium to high risk.
Measurements from #422
All from macOS arm64 with Python 3.14, with the plan built on the thread for the first time:
- A release build crashed at about 140 levels of loops or
c-ifon a 128 KiB thread. - A debug build crashed at 8 loops on a 96 KiB thread. Reading Python's plan description uses the most stack per level: about 11 KiB in a debug build and about 1 KiB in a release build.
When to do this
Do this when someone reports a template past the limit that lost the native path, or when profiling shows such templates matter.
Activity