Skip to content

feat(shutdown): implement graceful shutdown for camera session#3650

Open
EthanDM wants to merge 3 commits intomrousavy:mainfrom
EthanDM:fix/shutdown-freeze-ios26
Open

feat(shutdown): implement graceful shutdown for camera session#3650
EthanDM wants to merge 3 commits intomrousavy:mainfrom
EthanDM:fix/shutdown-freeze-ios26

Conversation

@EthanDM
Copy link

@EthanDM EthanDM commented Oct 18, 2025

What

Fix the navigation freeze on React Navigation/Fabric by shutting the camera session down deterministically when the view unmounts.

Changes

  • Add CameraSession.shutdown, which disables photo/video/audio/code-scanner/location/torch before flipping isActive = false, and clears delegates while removing outputs.
  • Stop both capture sessions asynchronously during CameraSession.deinit as a backstop.
  • Have CameraView call the shutdown helper on unmount/deinit, log if teardown exceeds 2 s, and guard against concurrent shutdowns.

Tested on

  • iPhone 14 Pro (iOS 26.0.1) with code scanner enabled, navigating away from the camera screen (no freeze).

Related issues

- Add shutdown method to stop all outputs and release resources
- Schedule shutdown with warning if it takes longer than expected
@vercel
Copy link

vercel bot commented Oct 18, 2025

@EthanDM is attempting to deploy a commit to the mrousavy's Team Team on Vercel.

A member of the Team first needs to authorize it.

@jkaufman
Copy link
Contributor

jkaufman commented Nov 8, 2025

@EthanDM Thank you for your effort on this. We’ve been testing a patch that builds on yours and would appreciate a look.

Copy link
Owner

@mrousavy mrousavy left a comment

Choose a reason for hiding this comment

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

Thanks for this PR - seems like there were some issues with proper session shutdown on Fabric - I wonder why that wasn't a problem before on Paper. Maybe view caching/recycling?

Anyways - I left some review comments

EthanDM and others added 2 commits November 23, 2025 09:27
Co-authored-by: Marc Rousavy <me@mrousavy.com>
- Remove unnecessary delegate assignments before removing outputs
- Ensure completion block is called in all error scenarios during configuration
@EthanDM
Copy link
Author

EthanDM commented Nov 23, 2025

Thanks for the review @mrousavy, I addressed all of the comments and retested. Still no freezes during mount/unmount.

Let me know if anything else is needed.

@stanrud
Copy link

stanrud commented Jan 6, 2026

Hey guys,
We implemented the same changes in our application and began to notice periodic application crashes in the production.
Below you can find more detailed information about the crash:

Fatal Exception: NSGenericException
*** -[AVCaptureSession stopRunning] stopRunning may not be called between calls to beginConfiguration and commitConfiguration

          Fatal Exception: NSGenericException
0  CoreFoundation                 0xc5964 (Missing UUID 0be54dbe1adc3588bffae7c99e8d8208)
1  libobjc.A.dylib                0x31814 (Missing UUID ee4a4347bd733beda7149cd0b16844a2)
2  AVFCapture                     0xe16c0 (Missing UUID 758d6ce7543c309eb5693e79a971aa9b)
3  Fleetback App                  0x9d85ac CameraSession.checkIsActive(configuration:) + 297 (CameraSession.swift:297)
4  Fleetback App                  0x9d812c closure #1 in CameraSession.configure(_:completion:) + 220 (CameraSession.swift:220)
5  Fleetback App                  0x124f8 <deduplicated_symbol>
6  libdispatch.dylib              0x1adc (Missing UUID 11c047c433b339a3ae43bf617cfc7d2c)
7  libdispatch.dylib              0x1b7fc (Missing UUID 11c047c433b339a3ae43bf617cfc7d2c)
8  libdispatch.dylib              0xa468 (Missing UUID 11c047c433b339a3ae43bf617cfc7d2c)
9  libdispatch.dylib              0xaf44 (Missing UUID 11c047c433b339a3ae43bf617cfc7d2c)
10 libdispatch.dylib              0x153ec (Missing UUID 11c047c433b339a3ae43bf617cfc7d2c)
11 libdispatch.dylib              0x14ce4 (Missing UUID 11c047c433b339a3ae43bf617cfc7d2c)
12 libsystem_pthread.dylib        0x13b8 (Missing UUID 741c5afb3f30362e9313aab3de2497c9)
13 libsystem_pthread.dylib        0x8c0 (Missing UUID 741c5afb3f30362e9313aab3de2497c9)

@nakshatrasinghh
Copy link

@stanrud I think you should try this patch out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Navigating away on React Native 0.78+, XCode 26, and React Native Vision Camera v4.7.1 freezes the app

5 participants