Fix: CameraDevice race condition crash on Android rotation (IllegalStateException)#80
Draft
Copilot wants to merge 2 commits into
Draft
Fix: CameraDevice race condition crash on Android rotation (IllegalStateException)#80Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
Add a previewSessionGeneration counter to MauiCameraView so that stale OnConfigured() callbacks—fired after the camera was closed and re-opened during rapid stop/start cycles (e.g. device rotation)—can detect they are obsolete and close their stale session instead of calling SetRepeatingRequest() on a CameraDevice that has already been closed. Race condition that was occurring: 1. Camera A opens → OnOpened sets cameraDevice=cam_A → StartPreview calls cam_A.CreateCaptureSession() (async) 2. StopCamera() (rotation) → cam_A.Close(), cameraDevice=null 3. Camera B opens → OnOpened sets cameraDevice=cam_B → StartPreview calls cam_B.CreateCaptureSession() (async) 4. *Old* OnConfigured(session_A) fires → overwrites previewSession with session_A → UpdatePreview sees cameraDevice=cam_B (not null!) → SetRepeatingRequest() on session_A (cam_A is closed) → CRASH Fix: - previewSessionGeneration atomically incremented at the start of each StartPreview() call; the captured value is passed to PreviewCaptureStateCallback - OnConfigured() checks that (a) cameraDevice != null and (b) its generation still matches the current counter; stale callbacks close the session and return without modifying previewSession - StartPreview() also bails out early after its await loop if the camera was stopped/restarted in the meantime - UpdatePreview() now also catches Java.Lang.IllegalStateException as a defensive safety net Agent-Logs-Url: https://github.com/janusw/CameraMaui/sessions/1a42b9b4-c323-4138-8b53-3540a3fcba5c Co-authored-by: janusw <484108+janusw@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix camera crash on device rotation in Android 15+
Fix: CameraDevice race condition crash on Android rotation (IllegalStateException)
Apr 21, 2026
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.
Rapid stop/start cycles during device rotation cause a stale
OnConfigured()callback to fire after a newCameraDevicehas already opened. The callback overwritespreviewSessionwith an old session tied to the already-closed device, then callsSetRepeatingRequest()on it →IllegalStateException: CameraDevice was already closed.Changes
Session generation counter —
previewSessionGeneration(atomically incremented viaInterlocked) is added toMauiCameraView; eachStartPreview()invocation captures its own generation value before anyawait.Stale callback detection in
OnConfigured()—PreviewCaptureStateCallbacknow stores the generation at construction time and guards against two stale conditions before touchingpreviewSession:Early exit in
StartPreview()— after theawait Task.Delayloop, bails out if the camera was stopped or a newerStartPreview()ran in the meantime.Defensive catch in
UpdatePreview()— addsJava.Lang.IllegalStateExceptionalongside the existingCameraAccessExceptioncatch as a last-resort safety net.