Skip to content

fix: prevent EXC_BAD_ACCESS crash on Cmd+Z by deferring syntax highlighting during undo (#650)#653

Merged
batonogov merged 3 commits intomainfrom
fix/undo-crash-650
Mar 30, 2026
Merged

fix: prevent EXC_BAD_ACCESS crash on Cmd+Z by deferring syntax highlighting during undo (#650)#653
batonogov merged 3 commits intomainfrom
fix/undo-crash-650

Conversation

@batonogov
Copy link
Copy Markdown
Owner

Summary

Fixes critical crash (EXC_BAD_ACCESS in objc_msgSend) when pressing Cmd+Z.

Root cause: undo manager modifies textStorage → textDidChange fires → debounced highlighting calls beginEditing/endEditing on same textStorage simultaneously → crash.

Fix: skip/defer syntax highlighting when undoManager.isUndoing || isRedoing.

Closes #650

Test plan

  • 9 unit tests pass
  • SwiftLint clean
  • Manual: edit file, Cmd+Z repeatedly — no crash

@batonogov batonogov added bug Something isn't working priority: high Critical priority editor Code editor related labels Mar 29, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 29, 2026

✅ Code Coverage: 70.3%

Threshold: 70%

Coverage is above the minimum threshold.

Generated by CI — see job summary for detailed file-level breakdown.

Copy link
Copy Markdown
Owner Author

@batonogov batonogov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVE. isUndoRedoInProgress корректно сбрасывается (в textDidChange + в deferred workItem). scheduleDeferredHighlight вынесен в общий метод. Реальный undo тест через undoManager.undo() присутствует. Двойная защита в SyntaxHighlighter (resetAttributes + applyMatches).

Race condition between undo manager and async syntax highlighting:
when undo/redo modifies NSTextStorage, the debounced highlighting
could also call beginEditing/endEditing on the same textStorage,
causing concurrent mutation and EXC_BAD_ACCESS.

Fix: detect isUndoing/isRedoing in textDidChange and defer syntax
highlighting until the undo manager completes. Also add guards in
SyntaxHighlighter.applyMatches() and resetAttributes() to bail out
if called during an active undo/redo operation.
@batonogov batonogov force-pushed the fix/undo-crash-650 branch from c34e7cc to 69c8b16 Compare March 29, 2026 19:46
@batonogov batonogov merged commit 1ca7864 into main Mar 30, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working editor Code editor related priority: high Critical priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: crash (EXC_BAD_ACCESS) on Cmd+Z undo in editor

1 participant