Skip to content

Conversation

@davidmartos96
Copy link
Contributor

As discussed in the linked issue, this is an attempt to palliate one of the thread race conditions. In particular, returning to Dart possibly before creating the session and then let other function like "startImageStream" create a different session in parallel and interleave, causing the java.lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface! error.

Fixes flutter/flutter#165092

cc @camsim99

Pre-Review Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I linked to at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.
  • I updated/added any relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or I have commented below to indicate which test exemption this PR falls under1.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2 3

@davidmartos96 davidmartos96 requested a review from camsim99 as a code owner March 20, 2025 17:28
Copy link
Contributor

@camsim99 camsim99 left a comment

Choose a reason for hiding this comment

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

This approach seems reasonable to me! Is it possible to add a test to ensure that sendCameraInitializedEvent gets called when expected?

@davidmartos96
Copy link
Contributor Author

davidmartos96 commented Mar 22, 2025

@camsim99 I did try to look into the testing part.
The problem I found is that in order to test that, we need many different types of mocks set up to go from Camera.open up to Camera.refreshPreviewCaptureSession, which calls createCaptureSessionWithSessionConfig and then resolves.

The other tests in the plugin don't need that many mocks as this one.
Is there a simpler method?

@davidmartos96
Copy link
Contributor Author

@camsim99 I managed to get a test working. Hopefully that test style with mockito is ok.

@davidmartos96 davidmartos96 requested a review from camsim99 March 27, 2025 00:26
Copy link
Contributor

@camsim99 camsim99 left a comment

Choose a reason for hiding this comment

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

Sorry for the delay on this. The test looks good to me, thanks!

@camsim99 camsim99 requested a review from stuartmorgan-g April 14, 2025 17:22
private void startRegularPreview() throws CameraAccessException {
private void startRegularPreview(@Nullable Runnable onSuccessCallback)
throws CameraAccessException {
if (pictureImageReader == null || pictureImageReader.getSurface() == null) return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are the early returns here and below catastrophic situations where never sending the initialization event is the correct behavior? It's at least theoretically a change in behavior from the previous implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I'm not familiar with the state of the camera plugin during initialization. Should that early return code path be treated as an error, throwing on the camera.initialize() or should it finish successfully?
Up until now camera.initialize() would not throw, but it seems that executing would mean we are initializing an already closed camera, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stuartmorgan-g I've updated those code paths, treating them as no-ops during initialization, like before.

Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

LGTM

@stuartmorgan-g stuartmorgan-g added the autosubmit Merge PR when tree becomes green via auto submit App label May 7, 2025
@auto-submit auto-submit bot merged commit b2ce3b0 into flutter:main May 7, 2025
82 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 7, 2025
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request May 7, 2025
flutter/packages@e800da7...b2ce3b0

2025-05-07 [email protected] [camera_android] Wait for creating
capture session when initializing (flutter/packages#8894)
2025-05-07 [email protected] [various] Delete discontinued
packages (flutter/packages#9215)
2025-05-07 [email protected] [camera_avfoundation]
Implementation swift migration - part 3 (flutter/packages#9182)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
androidseb pushed a commit to androidseb/packages that referenced this pull request Jun 8, 2025
…lutter#8894)

As discussed in the linked issue, this is an attempt to palliate one of the thread race conditions. In particular, returning to Dart possibly before creating the session and then let other function like "startImageStream" create a different session in parallel and interleave, causing the `java.lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!` error.

Fixes flutter/flutter#165092

cc @camsim99 

## Pre-Review Checklist

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
FMorschel pushed a commit to FMorschel/packages that referenced this pull request Jun 9, 2025
…lutter#8894)

As discussed in the linked issue, this is an attempt to palliate one of the thread race conditions. In particular, returning to Dart possibly before creating the session and then let other function like "startImageStream" create a different session in parallel and interleave, causing the `java.lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!` error.

Fixes flutter/flutter#165092

cc @camsim99 

## Pre-Review Checklist

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
Ortes pushed a commit to Ortes/packages that referenced this pull request Jun 25, 2025
…lutter#8894)

As discussed in the linked issue, this is an attempt to palliate one of the thread race conditions. In particular, returning to Dart possibly before creating the session and then let other function like "startImageStream" create a different session in parallel and interleave, causing the `java.lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!` error.

Fixes flutter/flutter#165092

cc @camsim99 

## Pre-Review Checklist

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
@davidmartos96 davidmartos96 deleted the fix_race_cond_android_stream branch October 27, 2025 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: camera platform-android

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible thread race conditions on camera_android initialization

3 participants