Skip to content

Fix TextBuilder stack overflow on iOS with many runs - #92

Open
andrewprocter wants to merge 1 commit into
gonzalezreal:mainfrom
andrewprocter:fix/balanced-text-concatenation
Open

andrewprocter wants to merge 1 commit into
gonzalezreal:mainfrom
andrewprocter:fix/balanced-text-concatenation

Conversation

@andrewprocter

Copy link
Copy Markdown
Contributor

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

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>
@andrewprocter

Copy link
Copy Markdown
Contributor Author

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. TextualRepro.xcodeproj uses 0.5.0 and TextualRepro-Fixed.xcodeproj uses this branch. Pick a line count and tap Render, and it shows a StructuredText with a long fenced json block. There are also two plain-SwiftUI renderers with no Textual involved: one joins runs a + b + c… and crashes, the other joins them as a balanced tree and doesn't.

On an iOS 27 simulator, 0.5.0 crashes at 250 lines of JSON (about 3,000 runs) with the same Text.resolve / ConcatenatedTextStorage.resolve recursion as our production crash. This branch renders the same block identically at that size and larger. A phone's main-thread stack is smaller, so a device crashes much sooner: our production report shows it failing at about 660 runs, which is roughly 55 lines of JSON.

🤖 Generated with Claude Code

y-a-v-a added a commit to y-a-v-a/textual that referenced this pull request Oct 22, 2026
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
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.

1 participant