Skip to content

fix(studio): render frames on a stack deep enough to deserialise them - #210

Merged
LeadcodeDev merged 1 commit into
mainfrom
fix/studio-render-stack
Aug 13, 2026
Merged

fix(studio): render frames on a stack deep enough to deserialise them#210
LeadcodeDev merged 1 commit into
mainfrom
fix/studio-render-stack

Conversation

@LeadcodeDev

Copy link
Copy Markdown
Owner

What happened

cargo run --bin rustmotion-studio aborted at startup on a deeply nested
scenario:

thread '<unknown>' (8987080) has overflowed its stack
fatal runtime error: stack overflow, aborting

The same file rendered fine through rustmotion render.

Why

The crash report puts the recursion in deserialisation, not in the paint
walk. prepare_scene calls deserialize_children on every frame, and
ChildComponentComponentContainer/Card recurses once per level of
nesting. Those enums are untagged, so serde buffers each level through
ContentDeserializer — every level costs stack as well as depth. A debug
build's frames are several times fatter than a release build's, which is why
this only shows up under cargo run.

Measured with the probe test below, on a scenario nested 28 levels:

stack debug release
512 KiB overflow ok
2 MiB overflow ok
4 MiB ok ok

Rust gives a spawned thread 2 MiB. So:

  • the prefetch workers overflowed — this is the thread in the crash report
  • the library thumbnail and editor preview asset handlers run on
    webview-owned threads, no better off
  • the CLI survived only because it happens to render on the 8 MiB main thread

28 levels is not pathological: a UI composed from small helper functions
(shell → body → content → col → row → badge) reaches it without trying.

The fix

All three render sites get an explicit stack. render_frame_deep is scoped,
so the scenario and tasks are borrowed rather than cloned per call; the
prefetch workers are sized once at spawn rather than per frame.

Not fixed here

deserialize_children runs per frame. That is a real cost on every render and
the reason the stack is deep in the first place — caching the deserialised tree
would fix both, but it is a bigger change than a crash fix should carry.

Opening a deeply nested scenario aborted the whole process with a stack
overflow, while the same file rendered fine from the CLI.

The depth cost is deserialisation, not painting: `prepare_scene` calls
`deserialize_children` on every frame, and `ChildComponent` -> `Component` ->
`Container`/`Card` recurses once per level. Those enums are untagged, so serde
buffers each level through `ContentDeserializer` — a level is expensive in stack
as well as deep, and a debug build's frames are several times fatter.

Measured on a 28-level scenario: between 2 and 4 MiB needed in debug. Rust
gives a spawned thread 2 MiB, so the prefetch workers overflowed; the CLI
survived only because it happens to render on the 8 MiB main thread. Both
webview asset handlers were exposed for the same reason.

All three render sites now get an explicit stack. `render_frame_deep` is scoped,
so the scenario and tasks are borrowed rather than cloned.

Adds a probe test that reports the stack a given scenario needs.
@LeadcodeDev LeadcodeDev added the bug Something isn't working label Aug 13, 2026
@LeadcodeDev LeadcodeDev self-assigned this Aug 13, 2026
@LeadcodeDev
LeadcodeDev merged commit 8f7275a into main Aug 13, 2026
3 checks passed
@LeadcodeDev
LeadcodeDev deleted the fix/studio-render-stack branch August 13, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant