Fix crash: stale code-block token cache after a programmatic content swap - #162
Open
secondstagehq wants to merge 1 commit into
Open
Fix crash: stale code-block token cache after a programmatic content swap#162secondstagehq wants to merge 1 commit into
secondstagehq wants to merge 1 commit into
Conversation
…tent swap updateCodeBlockSelection substrings the current text with ranges from cachedCodeBlockTokens, but only the typing/caret delegate paths refresh that cache. A programmatic content swap (document switch, external binding change) goes through rebuildTextStorageAndStyle, which left the cache untouched; the deferred no-parsed refresh in updateNSView then indexed the new text with the previous document's ranges and threw NSRangeException in -[NSString substringWithRange:] — an uncaught ObjC exception on the main queue, so the app aborts. Two layers: - rebuildTextStorageAndStyle now hands its own parse's code-block tokens to the cache (and clears it in raw source mode), so the deferred refresh always reads ranges parsed from the text it is shown with. - updateCodeBlockSelection skips any cached block whose range no longer fits the current text instead of substringing it, so no caller racing a content mutation can abort the process. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Crash
updateCodeBlockSelectioncuts each fenced block's code out of the current text with ranges fromcachedCodeBlockTokens:That cache is only refreshed by the typing/caret delegate paths. A programmatic content swap — a document switch, or the SwiftUI
textbinding changing externally — goes throughrebuildTextStorageAndStyle, which never touches the cache. The deferred refresh inupdateNSView:then runs with no
parsed, indexes the new (shorter) string with the previous document's ranges, and-[NSString substringWithRange:]throwsNSRangeException. Uncaught ObjC exception in a main-queue dispatch block →abort(). We hit this in production (macOS app embedding the engine, document with a fenced code block replaced by shorter content via sync) — crash report points exactly atNativeTextViewCoordinator.updateCodeBlockSelection←closure #7 in NativeTextViewWrapper.updateNSView. Reproduced in a unit test (included); still present on 0.12.0 (the file is unchanged).Fix (two layers)
rebuildTextStorageAndStylealready computes a parse of the incoming text; hand itscodeBlockTokensWithIndicesto the cache there (and clear the cache in raw source mode), so the deferred refresh always reads ranges parsed from the text it is shown with.updateCodeBlockSelectionskips any cached block whoserange/contentRangeno longer fits the current text instead of substringing it, so no caller racing a content mutation can abort the process. The next parse re-delivers the block.Includes a regression test suite (
CodeBlockSelectionStaleCacheTests) that reproduces the abort on unpatched code and verifies both layers. Full package suite passes (298 tests).The branch is based on the 0.11.0 tag (the version we ship); happy to rebase onto
mainif you prefer.🤖 Generated with Claude Code