-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
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.
- 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)
- 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!