X Tutup
Skip to content

Prevent deadlock on Android camera#113209

Merged
akien-mga merged 1 commit intogodotengine:masterfrom
shiena:fix/prevent_freeze_android_camera
Nov 27, 2025
Merged

Prevent deadlock on Android camera#113209
akien-mga merged 1 commit intogodotengine:masterfrom
shiena:fix/prevent_freeze_android_camera

Conversation

@shiena
Copy link
Contributor

@shiena shiena commented Nov 26, 2025

fix #113208

The deadlock occurs when switching cameras due to a lock ordering issue between the main thread and the camera callback thread:

Scenario:

  1. Main thread calls deactivate_feed() to switch cameras
  2. ACameraCaptureSession_stopRepeating() is called, which may wait for pending callbacks to complete
  3. Meanwhile, onImage callback is running on the camera thread and holds callback_mutex
  4. stopRepeating() internally waits for the callback to finish
  5. Main thread then tries to acquire callback_mutex → Deadlock

Root cause: The original code called AImageReader_setImageListener(reader, nullptr) after stopRepeating, allowing callbacks to continue firing while the session was being stopped.

Fix

Reorder the cleanup sequence in deactivate_feed():

Why this works:

  • Removing the image listener first ensures no new onImage callbacks are dispatched
  • stopRepeating and close can safely wait for any in-flight callbacks to complete
  • By the time we acquire callback_mutex, no callbacks are running or pending

Copy link
Contributor

@m4gr3d m4gr3d left a comment

Choose a reason for hiding this comment

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

The code looks good, thanks for the fix!

@m4gr3d m4gr3d added this to the 4.6 milestone Nov 26, 2025
@akien-mga akien-mga changed the title Prevent deadlock on android camera Prevent deadlock on Android camera Nov 26, 2025
@akien-mga akien-mga merged commit 3a97723 into godotengine:master Nov 27, 2025
20 checks passed
@akien-mga
Copy link
Member

Thanks!

@shiena shiena deleted the fix/prevent_freeze_android_camera branch November 27, 2025 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Switching between back and front camera freezes the app

3 participants

X Tutup