Skip to content

Fix: OCR crash on screen capture enable (array bounds failure) - #6252

Merged
kodjima33 merged 1 commit into
mainfrom
fix/ocr-crash-array-bounds
Apr 1, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
fix/ocr-crash-array-bounds

Conversation

@kodjima33

Copy link
Copy Markdown
Collaborator

Problem

macOS app crashes with EXC_BREAKPOINT in __SwiftNativeNSArrayWithContiguousStorage._objectAt when Screen Capture is enabled. 100% reproducible on macOS 15.7+ and macOS Tahoe 26.4.

Sentry: 32+ affected users, 299 events (OMI-COMPUTER-57)

Root Cause

VNRecognizeTextRequest completion handler iterates a bridged NSArray that can have its backing store released mid-enumeration, causing a Swift runtime assertion failure.

Fix

  • Defensive copy of observations array before iteration (breaks NSArray bridging dependency)
  • Emptiness check on topCandidates() before accessing .first
  • Finite validation on bounding box values before arithmetic

Minimal, surgical change — 12 lines added, 2 lines removed in a single file.

Fixes #5891
Fixes #5151

The VNRecognizeTextRequest completion handler accesses bridged NSArray
results that can trigger EXC_BREAKPOINT (_objectAt assertion failure)
when the underlying buffer is mutated during enumeration.

Changes:
- Copy observations array before iterating (breaks NSArray bridging dependency)
- Add emptiness check on topCandidates before accessing .first
- Validate boundingBox values are finite before arithmetic

Fixes #5891, fixes #5151
@kodjima33 kodjima33 added bug Something isn't working desktop labels Apr 1, 2026
@greptile-apps

greptile-apps Bot commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR applies a minimal, surgical fix to RewindOCRService.swift to eliminate the EXC_BREAKPOINT crash (__SwiftNativeNSArrayWithContiguousStorage._objectAt) that affected 32+ users when Screen Capture was enabled. All three changes are correct and well-motivated.

Key changes:

  • Defensive Array() copy (line 180): request.results is a lazily-bridged NSArray whose backing store can be released from another thread mid-enumeration. Calling Array(observations) synchronously within the completion handler materialises all elements into a Swift-owned array before any deallocation can race with the subsequent loop — this is the primary fix.
  • isEmpty + first guard (lines 186–187): Adds an explicit emptiness check before topCandidates(1).first. The extra !candidates.isEmpty is technically redundant (Swift's first already returns nil for empty arrays and topCandidates returns a native Swift array, not a bridged NSArray), but it's harmless.
  • Finite bounding-box guard (lines 191–192): Validates all four CGFloat fields before arithmetic. Apple's Vision framework is unlikely to produce non-finite values in practice, but this is a reasonable belt-and-suspenders defence against malformed or hardware-accelerated edge cases.

No logic regressions are introduced; the rest of the actor is untouched.

Confidence Score: 5/5

Safe to merge — minimal change that correctly addresses a confirmed crash affecting 32+ users, with no regressions.

All three changes are defensive and correct. The only finding is a P2 style suggestion (redundant !candidates.isEmpty check) that does not affect correctness, safety, or runtime behaviour. The primary fix (Array(observations)) directly breaks the NSArray bridging dependency that caused the EXC_BREAKPOINT crash.

No files require special attention.

Important Files Changed

Filename Overview
desktop/Desktop/Sources/Rewind/Core/RewindOCRService.swift Defensive copy of bridged NSArray before enumeration, emptiness guard on topCandidates(), and finite-value validation on bounding box — surgical, correct fix for the EXC_BREAKPOINT crash (32+ users, 299 Sentry events)

Sequence Diagram

sequenceDiagram
    participant Caller
    participant RewindOCRService
    participant VNImageRequestHandler
    participant VNRecognizeTextRequest
    participant NSArray as "Bridged NSArray (request.results)"
    participant SafeArray as "Swift Array (safeObservations)"

    Caller->>RewindOCRService: extractTextWithBounds(cgImage)
    RewindOCRService->>VNRecognizeTextRequest: create + configure
    RewindOCRService->>VNImageRequestHandler: perform([request])
    VNImageRequestHandler->>VNRecognizeTextRequest: completion(request, error)
    VNRecognizeTextRequest->>NSArray: request.results as? [VNRecognizedTextObservation]
    Note over NSArray: Backing store may be released mid-enumeration
    VNRecognizeTextRequest->>SafeArray: Array(observations) defensive copy (NEW)
    Note over SafeArray: Swift-owned memory, safe to enumerate
    loop for observation in safeObservations
        SafeArray->>SafeArray: topCandidates(1).first
        SafeArray->>SafeArray: guard boundingBox values isFinite (NEW)
        SafeArray->>SafeArray: build OCRTextBlock
    end
    VNRecognizeTextRequest->>RewindOCRService: continuation.resume(returning: OCRResult)
    RewindOCRService->>Caller: OCRResult
Loading

Reviews (1): Last reviewed commit: "Fix OCR crash: defensive copy of Vision ..." | Re-trigger Greptile

Comment on lines +186 to +187
let candidates = observation.topCandidates(1)
guard !candidates.isEmpty, let candidate = candidates.first else { continue }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Redundant isEmpty pre-check before candidates.first

The !candidates.isEmpty guard is redundant here. candidates.first already returns nil for an empty array, so the existing let candidate = candidates.first guard already handles that path. The extra check adds no safety and slightly obscures intent. Unlike observations (which was a lazily-bridged NSArray), topCandidates(1) returns a freshly constructed [VNRecognizedText] Swift value, so there's no bridging concern to protect against.

Suggested change
let candidates = observation.topCandidates(1)
guard !candidates.isEmpty, let candidate = candidates.first else { continue }
guard let candidate = candidates.first else { continue }

@kodjima33
kodjima33 merged commit f054747 into main Apr 1, 2026
3 checks passed
@kodjima33
kodjima33 deleted the fix/ocr-crash-array-bounds branch April 1, 2026 19:27
Glucksberg pushed a commit to Glucksberg/omi-local that referenced this pull request Apr 28, 2026
…Hardware#6252)

## Problem
macOS app crashes with `EXC_BREAKPOINT` in
`__SwiftNativeNSArrayWithContiguousStorage._objectAt` when Screen
Capture is enabled. 100% reproducible on macOS 15.7+ and macOS Tahoe
26.4.

**Sentry:** 32+ affected users, 299 events
([OMI-COMPUTER-57](https://mediar-n5.sentry.io/issues/7240539156/))

## Root Cause
`VNRecognizeTextRequest` completion handler iterates a bridged `NSArray`
that can have its backing store released mid-enumeration, causing a
Swift runtime assertion failure.

## Fix
- **Defensive copy** of `observations` array before iteration (breaks
NSArray bridging dependency)
- **Emptiness check** on `topCandidates()` before accessing `.first`  
- **Finite validation** on bounding box values before arithmetic

Minimal, surgical change — 12 lines added, 2 lines removed in a single
file.

Fixes BasedHardware#5891
Fixes BasedHardware#5151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working desktop

Projects

None yet

1 participant