Repository navigation
Fix TextBuilder stack overflow on iOS with many runs - #92
andrewprocter wants to merge 1 commit into
Conversation
Joining runs left to right nests the concatenated Text one level per run, and SwiftUI resolves it recursively. A syntax-highlighted code block with a few hundred tokens exhausts the 1 MB main-thread stack on iOS. Join the runs as a balanced tree so the depth is logarithmic, and raise the regression test to a run count that overflows macOS's 8 MB main-thread stack without the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Here's a standalone repro: https://github.com/andrewprocter/textual-stack-overflow-repro It's a one-screen iOS app with two projects sharing the same source. On an iOS 27 simulator, 0.5.0 crashes at 250 lines of JSON (about 3,000 runs) with the same 🤖 Generated with Claude Code |
Upstream main has had no new commits since 0.5.0 (2026-06-15), which the fork already contains. The activity since then is in open pull requests; the fixes among them are merged here, cherry-picked with their authorship and adapted where they overlap with the fork: - gonzalezreal#67, gonzalezreal#70, gonzalezreal#71, gonzalezreal#80: selection faults and crashes - gonzalezreal#92: TextBuilder stack overflow on iOS - gonzalezreal#83: caller attributes kept on code blocks - gonzalezreal#86: stale content after markup changes, keeping loaded attachments while streamed content resolves - gonzalezreal#91: attachment accessibility labels - gonzalezreal#84, gonzalezreal#90: content on the first layout pass, so List rows keep their height and ImageRenderer draws text Also fixes a fork bug the gonzalezreal#80 tests exposed (the selection model ignored a change to an empty layout collection) and two timing-dependent tests. Feature pull requests (gonzalezreal#69, gonzalezreal#78, gonzalezreal#79, gonzalezreal#81, gonzalezreal#82, gonzalezreal#88) are left out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y7W6e4M3bCGaGpJzVY8c39
Problem
Our iOS app crashes when it shows a syntax-highlighted code block of a few dozen lines. The crash is a stack overflow on the main thread, deep in SwiftUI's `Text.resolve` / `ConcatenatedTextStorage.resolve`, repeated hundreds of times.
Why
`TextBuilder` joins a block's runs one after another (`a + b + c + …`), which builds a chain nested one level per run. SwiftUI walks that chain recursively when it draws, so a block with N runs costs N levels of stack. Highlighted code has one run per token, so a modest snippet produces hundreds of runs. #65 made each level cheaper, but the depth still grows with the run count. The 2,500-run test passes on macOS because the main thread there has an 8 MB stack. An iPhone's main thread has 1 MB, and on iOS 27 our crash report shows it running out at about 660 runs.
Fix
Join the runs as a balanced tree: each half is joined first, then the two halves. The output is the same text with the same attributes, but the nesting depth is now about log₂(N), roughly 15 for 20,000 runs.
The regression test now uses 20,000 runs, which crashes macOS's 8 MB stack without this change (signal 11) and passes with it. All 138 tests pass, snapshot tests included.
🤖 Generated with Claude Code