Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[camera] Avoid joining thread causing ANR #6224

Merged
merged 8 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/camera/camera_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.10.0+2

* Removes call to `join` on the camera's background `HandlerThread`.
* Updates minimum Flutter version to 2.10.

## 0.10.0+1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ class Camera

/** An additional thread for running tasks that shouldn't block the UI. */
private HandlerThread backgroundHandlerThread;
/** True when backgroundHandlerThread is in the process of being stopped. */
private boolean stoppingBackgroundHandlerThread = false;

private CameraDeviceWrapper cameraDevice;
private CameraCaptureSession captureSession;
Expand Down Expand Up @@ -671,21 +669,11 @@ public void startBackgroundThread() {

/** Stops the background thread and its {@link Handler}. */
public void stopBackgroundThread() {
if (stoppingBackgroundHandlerThread) {
return;
}
if (backgroundHandlerThread != null) {
stoppingBackgroundHandlerThread = true;
backgroundHandlerThread.quitSafely();
try {
backgroundHandlerThread.join();
Copy link
Contributor

Choose a reason for hiding this comment

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

What does stoppingBackgroundHandlerThread do without this join? Presumably from the description quitSafely is asynchronous, so it looks like we're now setting it to true and then back to false within the same execution cycle.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh you're right. I think I added that not knowing it was acting as a bandaid for the real problem!

} catch (InterruptedException e) {
dartMessenger.error(flutterResult, "cameraAccess", e.getMessage(), null);
}
}
backgroundHandlerThread = null;
backgroundHandler = null;
stoppingBackgroundHandlerThread = false;
}

/** Start capturing a picture, doing autofocus first. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,25 +838,12 @@ public void startBackgroundThread_shouldNotStartNewThreadWhenAlreadyCreated() {
}

@Test
public void stopBackgroundThread_cancelsDuplicateCalls() throws InterruptedException {
TestUtils.setPrivateField(camera, "stoppingBackgroundHandlerThread", true);

camera.startBackgroundThread();
camera.stopBackgroundThread();

verify(mockHandlerThread, never()).quitSafely();
verify(mockHandlerThread, never()).join();
}

@Test
public void stopBackgroundThread_proceedsWithoutDuplicateCall() throws InterruptedException {
TestUtils.setPrivateField(camera, "stoppingBackgroundHandlerThread", false);

public void stopBackgroundThread_quitsSafely() throws InterruptedException {
camera.startBackgroundThread();
camera.stopBackgroundThread();

verify(mockHandlerThread).quitSafely();
verify(mockHandlerThread).join();
verify(mockHandlerThread, never()).join();
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_android
description: Android implementation of the camera plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.10.0+1
version: 0.10.0+2

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down