fix: prevent unicode corruption from thread-unsafe OCR mutation#1388
Open
Project-A-E-G-I-S wants to merge 1 commit into
Open
fix: prevent unicode corruption from thread-unsafe OCR mutation#1388Project-A-E-G-I-S wants to merge 1 commit into
Project-A-E-G-I-S wants to merge 1 commit into
Conversation
HistoryItem.generateTitle() fires a Task on the default executor to perform text recognition via Vision. VNImageRequestHandler.perform() is synchronous, so the recognizeTextHandler completion handler also runs on that background thread. It was directly mutating self.title on a SwiftData @model object — a thread safety violation that gradually corrupts the ModelContext'\''s internal state, affecting ALL loaded objects (not just images). This explains the random unicode corruption reported in p0deje#1374: over days of use, the accumulated corruption spreads to all clipboard items. Quitting the app clears the in-memory context, which is why it appears to '\''fix'\'' temporarily. Fix: Dispatch the self.title mutation to @mainactor. Closes p0deje#1374
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.
Summary
Fixes a thread safety violation where
VNRecognizeTextRequest's completion handler mutated a SwiftData@Modelproperty from a background thread, gradually corrupting theModelContext's internal state and causing random unicode corruption across all clipboard items.Root Cause
In
HistoryItem.generateTitle()(Maccy/Models/HistoryItem.swift:88-94), when the copied item is an image, aTaskfiresperformTextRecognition(). SincegenerateTitle()is not@MainActor, this Task runs on the default global concurrent executor.VNImageRequestHandler.perform()is synchronous (Maccy/Models/HistoryItem.swift:226) — therecognizeTextHandlercompletion handler executes on the same background thread. It directly setself.titleon a SwiftData@Modelobject (line 241), which is only safe on the@MainActor.This thread safety violation corrupts the shared
ModelContext's internal snapshot and change tracking state. Over days of use, the corruption spreads to all loaded objects — not just images — manifesting as random unicode characters in clipboard text.Quitting the app clears the in-memory context, which is why it temporarily 'fixes' itself on relaunch.
Fix
Capture the recognized text on the background thread (safe), then dispatch the SwiftData model mutation to
@MainActor:Testing
Closes #1374