react-native-vision-camera icon indicating copy to clipboard operation
react-native-vision-camera copied to clipboard

iOS SIGTRAP: prepareSettings double-resumes Swift continuation when AVFoundation fires setPreparedPhotoSettingsArray twice

Open rfree18 opened this issue 3 months ago • 0 comments

Description

Calling photoOutput.prepareSettings(...) on iOS can crash the process with EXC_BREAKPOINT / SIGTRAP (TRAP_BRKPT) when the camera session is still starting or being reconfigured.

Environment

  • react-native-vision-camera 5.2.1
  • iOS 26.x (also reproducible pattern on App Store crash reports)
  • Photo + other outputs on the same session (we also attach a text-recognition video output)

Stack (innermost frames)

AVCapturePhotoOutput _handlePreparationCompleteNotificationWithPayload:settingsID:
@objc completion handler block for (Bool, NSError?) -> () "zero on error"
specialized _resumeUnsafeThrowingContinuationWithError
specialized UnsafeContinuation.resume(throwing:)   ← traps here

Root cause

In HybridCameraPhotoOutput.prepareSettings:

try await self.output.setPreparedPhotoSettingsArray(captureSettings)

This uses Swift's compiler-generated bridge over the ObjC completion-handler API. AVFoundation can invoke that completion handler a second time (often with a non-nil NSError) when an in-flight preparation is superseded by session reconfiguration / startRunning. The second resume of the same UnsafeContinuation is a Swift runtime fatal error → SIGTRAP.

Internal prepareDefaultPhotoSettings() uses the fire-and-forget overload (no completion handler) and is safe. Only the JS-exposed async prepareSettings path is vulnerable.

Repro shape

  1. Mount <Camera> with a photo output
  2. Call prepareSettings([...]) on mount (races AVCaptureSession.startRunning)
  3. Optionally remount / toggle isActive / change formats so the session reconfigures while preparation is in flight

Suggested fix

Guard the continuation so a duplicate callback is ignored, e.g. resume-once with OSAllocatedUnfairLock / atomic flag around withCheckedThrowingContinuation instead of bare try await setPreparedPhotoSettingsArray.

Workaround

Do not call prepareSettings from JS; capturePhotoToFile still works without it (slightly slower first capture after flash settings change).

rfree18 avatar Aug 11 '26 17:08 rfree18