Skip to content

Possible thread race conditions on camera_android initialization #165092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
davidmartos96 opened this issue Mar 12, 2025 · 9 comments · May be fixed by flutter/packages#8894
Open

Possible thread race conditions on camera_android initialization #165092

davidmartos96 opened this issue Mar 12, 2025 · 9 comments · May be fixed by flutter/packages#8894
Labels
p: camera The camera plugin P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-android Android applications specifically team-android Owned by Android platform team triaged-android Triaged by Android platform team

Comments

@davidmartos96
Copy link
Contributor

davidmartos96 commented Mar 12, 2025

What package does this bug report belong to?

camera

What target platforms are you seeing this bug on?

Android

Have you already upgraded your packages?

Yes

Dependency versions

No response

Steps to reproduce

There are 2 Android native crashes I'm seeing very often on our Crashlytics. Unfortunately none of them replicable on our devices.

  1. Crash when the user goes back to the foreground. We close the camera and init when going to the paused/resumed state in the app lifecycle
java.lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!
    at android.hardware.camera2.CaptureRequest.convertSurfaceToStreamId(CaptureRequest.java:765)
    at android.hardware.camera2.impl.CameraDeviceImpl.submitCaptureRequest(CameraDeviceImpl.java:1307)
    at android.hardware.camera2.impl.CameraDeviceImpl.setRepeatingRequest(CameraDeviceImpl.java:1363)
    at android.hardware.camera2.impl.CameraCaptureSessionImpl.setRepeatingRequest(CameraCaptureSessionImpl.java:313)
    at D3.v.g0(SourceFile:34)
    at D3.v$b.onConfigured(SourceFile:42)
    at android.hardware.camera2.impl.CallbackProxies$SessionStateCallbackProxy.lambda$onConfigured$0(CallbackProxies.java:53)
    at android.hardware.camera2.impl.CallbackProxies$SessionStateCallbackProxy.$r8$lambda$XOBHWVfryPuKTX27fDYaiDBtLvc
    at android.hardware.camera2.impl.CallbackProxies$SessionStateCallbackProxy$$ExternalSyntheticLambda6.run
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
    at java.lang.Thread.run(Thread.java:1012)
  1. NullpointerException in the following line
    https://github.com/flutter/packages/blob/9cc6f370eff889b2d7b360b8ae586a3fec5a7f92/packages/camera/camera_android/android/src/main/java/io/flutter/plugins/camera/Camera.java#L1288
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.camera2.CameraCaptureSession.close()' on a null object reference
  at io.flutter.plugins.camera.Camera.closeCaptureSession (Camera.java:1232)
  at io.flutter.plugins.camera.Camera$1.onClosed (Camera.java:347)
  at android.hardware.camera2.impl.CameraDeviceImpl$5.run (CameraDeviceImpl.java:234)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loopOnce (Looper.java:210)
  at android.os.Looper.loop (Looper.java:299)
  at android.os.HandlerThread.run (HandlerThread.java:67)

Our camera code is just for image processing so it's basically the following. The example app could be used for debugging purposes.

final controller = CameraController(...);
await controller.initialize();
await controller.startImageStream(...);

After digging a bit into the code I think I know what the cause could be. Initialize on the native side will create a captureSession asynchronously on a thread, but it responds to the Dart plugin possibly earlier than when the session gets configured. Then startImageStream will call createCaptureSession on the main thread. This situation could result in the 2 threads overlapping the initialization of the 2 capture sessions, possibly creating the first error exception.
https://github.com/flutter/packages/blob/9cc6f370eff889b2d7b360b8ae586a3fec5a7f92/packages/camera/camera_android/android/src/main/java/io/flutter/plugins/camera/Camera.java#L371

The second error also hints a threading issue because the null pointer happens right after checking for null.

I believe that a simple fix for 1 would be to reply to the Dart side when the session is configured, and not right after startPreview is called asynchronously.

The second error, could also be related to the onClosed callback in the "initialize" section not running on the main thread.

I know that the camerax plugin exists, but we are dealing with critical image recognition code running on many different devices. We are waiting a bit until the camerax plugin gets more mature.
I've read on other cameraX realted issue that there are currently some threading issues, which would be fixed by flutter/packages#8618 . Is this related, but on the original camera_android plugin?

Expected results

No crashes

Actual results

Crashes on a few situations, possible race conditions.

Code sample

The example app can be used, and simply add the streaming right after calling initialize.

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.1, on Fedora Linux 41 (Workstation Edition) 6.13.5-200.fc41.x86_64, locale en_US.UTF-8) [85ms]
    • Flutter version 3.29.1 on channel stable at /home/david/.local/share/mise/installs/flutter/3.29.1-stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 09de023485 (12 days ago), 2025-02-28 13:44:05 -0800
    • Engine revision 871f65ac1b
    • Dart version 3.7.0
    • DevTools version 2.42.2

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [1,096ms]
    • Android SDK at /home/david/Android/Sdk/
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /home/david/opt/android-studio/jbr/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.5+-13047016-b750.29)
    • All Android licenses accepted.

[✓] Chrome - develop for the web [17ms]
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop [161ms]
    • clang version 19.1.7 (Fedora 19.1.7-3.fc41)
    • cmake version 3.30.8
    • ninja version 1.12.1
    • pkg-config version 2.3.0

[✓] Android Studio (version 2024.2) [15ms]
    • Android Studio at /home/david/opt/android-studio/
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = /home/david/opt/android-studio/
    • Java version OpenJDK Runtime Environment (build 21.0.5+-13047016-b750.29)

[✓] VS Code (version 1.98.0) [14ms]
    • VS Code at /usr/share/code
    • Flutter extension version 3.106.0

[✓] Connected device (2 available) [125ms]
    • Linux (desktop) • linux  • linux-x64      • Fedora Linux 41 (Workstation Edition) 6.13.5-200.fc41.x86_64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 134.0.6998.35

[✓] Network resources [249ms]
    • All expected network resources are available.

• No issues found!
@davidmartos96 davidmartos96 changed the title Possible thread race condition on camera_android initialization Possible thread race conditions on camera_android initialization Mar 12, 2025
@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Mar 13, 2025
@darshankawar
Copy link
Member

@davidmartos96 Thanks for detailed report.
Please check this #128414 related to StartImageStream which you are also implementing, probably it is getting stuck duringStartImageStream itself.

Another issue that was fixed #125314 that you can check if it helps to replicate the behavior.

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Mar 13, 2025
@davidmartos96
Copy link
Contributor Author

Thank you! I wanted to open a separate issue, as those linked ones use different features like capturing a photo.
On our experience with the camera package is just when using initialized+startStream one after the other.
Possibly helping reduce the scope of discovering race conditions

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Mar 13, 2025
@darshankawar
Copy link
Member

Thanks for the update. I tried to replicate using the given details using camera plugin example running on S10+ device and also tried the fixed linked issue but was unable to replicate, but based on the report and finding, I'll keep the issue open and label for team's tracking.

@darshankawar darshankawar added platform-android Android applications specifically p: camera The camera plugin package flutter/packages repository. See also p: labels. team-android Owned by Android platform team and removed in triage Presently being triaged by the triage team labels Mar 14, 2025
@reidbaker reidbaker added triaged-android Triaged by Android platform team P3 Issues that are less important to the Flutter project labels Mar 18, 2025
@reidbaker
Copy link
Contributor

Pull requests welcome please add @camsim99 as a reviewer if you (or someone else) opens one.

The bulk of our camera related time right now is spent on improving the feature set and stability of camera_android_camerax.

You might also try that. If you cant because a feature is missing please let me know what feature.

@changkuanlai
Copy link

hi my google play error
Exception java.lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!
at android.hardware.camera2.CaptureRequest.convertSurfaceToStreamId (CaptureRequest.java:765)
at android.hardware.camera2.impl.CameraDeviceImpl.submitCaptureRequest (CameraDeviceImpl.java:1307)
at android.hardware.camera2.impl.CameraDeviceImpl.setRepeatingRequest (CameraDeviceImpl.java:1363)
at android.hardware.camera2.impl.CameraCaptureSessionImpl.setRepeatingRequest (CameraCaptureSessionImpl.java:313)
at io.flutter.plugins.camera.Camera.refreshPreviewCaptureSession (Camera.java:33)
at io.flutter.plugins.camera.Camera$2.onConfigured (Camera.java:41)
at android.hardware.camera2.impl.CallbackProxies$SessionStateCallbackProxy.lambda$onConfigured$0 (CallbackProxies.java:53)
at android.hardware.camera2.impl.CallbackProxies$SessionStateCallbackProxy.$r8$lambda$XOBHWVfryPuKTX27fDYaiDBtLvc (Unknown Source)
at android.hardware.camera2.impl.CallbackProxies$SessionStateCallbackProxy$$ExternalSyntheticLambda6.run (Unknown Source:4)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:644)
at java.lang.Thread.run (Thread.java:1012)

Image

@davidmartos96
Copy link
Contributor Author

@changkuanlai I've used my fix from the linked PR in my last app update and those errors disappeared. Feel free to try it out with a dependency override

Now I'm only seeing the other error I described above, the NullPointerException.

@changkuanlai

This comment has been minimized.

@davidmartos96
Copy link
Contributor Author

@camsim99 I've been thinking a bit on possible solutions for the second described thread race conditions in the original post.
Do you think this would be an appropriate solution? It would be an easy fix and low risk. I was unable to reproduce it in my devices, but it's definitely common in the wild. The fix basically closes a non null object by keeping it as a local variable before doing the if not null check.

SkillDevs/flutter_packages@36278aa

@camsim99
Copy link
Contributor

SkillDevs/flutter_packages@36278aa

@davidmartos96 This seems perfectly reasonable to try out to me! If you open a PR, I'll help land it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: camera The camera plugin P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. platform-android Android applications specifically team-android Owned by Android platform team triaged-android Triaged by Android platform team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants