iOS SIGTRAP: prepareSettings double-resumes Swift continuation when AVFoundation fires setPreparedPhotoSettingsArray twice
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-camera5.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
- Mount
<Camera>with a photo output - Call
prepareSettings([...])on mount (racesAVCaptureSession.startRunning) - 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).