-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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
Comments
@davidmartos96 Thanks for detailed report. Another issue that was fixed #125314 that you can check if it helps to replicate the behavior. |
Thank you! I wanted to open a separate issue, as those linked ones use different features like capturing a photo. |
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. |
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. |
hi my google play error |
@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. |
This comment has been minimized.
This comment has been minimized.
@camsim99 I've been thinking a bit on possible solutions for the second described thread race conditions in the original post. |
@davidmartos96 This seems perfectly reasonable to try out to me! If you open a PR, I'll help land it. |
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.
https://github.com/flutter/packages/blob/9cc6f370eff889b2d7b360b8ae586a3fec5a7f92/packages/camera/camera_android/android/src/main/java/io/flutter/plugins/camera/Camera.java#L1288
Our camera code is just for image processing so it's basically the following. The example app could be used for debugging purposes.
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
The text was updated successfully, but these errors were encountered: