Skip to content

[camera] Dispose resources correctly on setDescription #4003

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

Merged
merged 13 commits into from
Sep 29, 2023

Conversation

BradenBagby
Copy link
Contributor

@BradenBagby BradenBagby commented May 15, 2023

setDescription for CameraController was not correctly handling resources. It would recreate the _deviceOrientationSubscription each time it was called, which caused the subscription to not be disposed correctly. It also was not disposing of the previous device camera

List which issues are fixed by this PR. You must list at least one issue.

Issue seen here: flutter/flutter#126823

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki 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 listed 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 this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

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

…en cameras, does not recreate _deviceOrientationSubscription each time
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@jellynoone
Copy link

Hi, I was the one that reported the issue. Thank you for fixing on this!

Are you sure doing await _initCalled actually does anything, from the looks of the class, _initCalled might be marked as FutureOr? but its only ever bool or null. (I think this fact also puts into question the dispose method of this class which uses the same logic, the _initCalled is never really awaited for since its never a Future, so if the initialisation process is going on it will be null not Future)

Additionally, what if a camera preview is being displayed and the camera is disposed before the new one is initialised? I don't know this is why I'm asking.

@BradenBagby
Copy link
Contributor Author

Hi, I was the one that reported the issue. Thank you for fixing on this!

Are you sure doing await _initCalled actually does anything, from the looks of the class, _initCalled might be marked as FutureOr? but its only ever bool or null. (I think this fact also puts into question the dispose method of this class which uses the same logic, the _initCalled is never really awaited for since its never a Future, so if the initialisation process is going on it will be null not Future)

Additionally, what if a camera preview is being displayed and the camera is disposed before the new one is initialised? I don't know this is why I'm asking.

Yeah thanks again! The fix was exactly what you laid out in your issue report.

Those are some good points. There probably should be some future for initializing that can be awaited so we don't accidentally overlap two initialization calls. It does look like _initCalled is never actually used as a future as you point out.

I think the CameraController class definitely needs some improvements. Feel free to add to this PR or submit another one, I will also dive into it when I get the chance!

// dispose resources from previous camera description
if (_initCalled != null) {
await _initCalled;
_unawaited(CameraPlatform.instance.dispose(_cameraId));
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like the idea that the CameraController calls dispose and is still remains usable. This doesn't seem like intended behavior. And now that I look at setDescription, is it possible to call setDescription instead of initialize() to initialize a camera? This also doesn't seem intended.

I think it makes more sense to put this line in setDescription and not have this conditional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ill change and test it with that under setDescription.

I agree its weird that you could call setDescription to completely initialize the controller. Since setDescription is really just handling what you used to have to do manually when changing cameras -> re-initialize a controller with a new description. We could throw calls to setDescription if initialize has never been called?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. I think we should add the _throwIfNotInitialized method to the top of setDescription.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you going to add _throwIfNotInitialized to the beginning of the method?

@stuartmorgan-g
Copy link
Contributor

@BradenBagby Are you still planning on updating this PR based on the feedback above?

@BradenBagby
Copy link
Contributor Author

Ive moved it to setDescription @bparrishMines . Let me know if you want setDescription to throw an error if its not already initialized instead of just initializing it for you. If thats the case, wouldn't need the dispose call within the if block, but we would probably still want to await _initCalled incase the user is spamming the switch camera button

Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

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

I added a couple more comments.

As a side note, I didn't realize before that setDescription reinitializes the CameraController. I don't think the class was designed to be able to handle this, so we are starting to see bugs like this pop up. It may be worth evaluating whether the setDescription should only be used while recording for the short term. This doesn't block this PR from fixing these bugs though.

@BradenBagby
Copy link
Contributor Author

Yes I also think it should just be setDescriptionWhileRecording. I should hopefully be able to get to fixing this pr in the next couple days

Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

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

This looks good to me except a missing test. Specifically, you could test that dispose is called in setDescription.

@stuartmorgan-g
Copy link
Contributor

Is this ready for a second review? (It looks like it's failing analysis, but it's not clear to me from discussion above if it's ready for a second look more generally.)

@stuartmorgan-g
Copy link
Contributor

Update from triage: @bparrishMines will add tests to this when time allows.

@bparrishMines
Copy link
Contributor

@stuartmorgan I added the tests and also updated _initCalled with a field that actually waits for initialization.

Copy link
Contributor

@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

@bparrishMines bparrishMines added autosubmit Merge PR when tree becomes green via auto submit App and removed needs tests labels Sep 29, 2023
@auto-submit
Copy link
Contributor

auto-submit bot commented Sep 29, 2023

auto label is removed for flutter/packages/4003, due to This PR has not met approval requirements for merging. You are not a member of flutter-hackers and need 1 more review(s) in order to merge this PR.

  • Merge guidelines: You need at least one approved review if you are already part of flutter-hackers or two member reviews if you are not a flutter-hacker before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Sep 29, 2023
Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

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

LGTM

@bparrishMines bparrishMines added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 29, 2023
@auto-submit auto-submit bot merged commit 8483ac2 into flutter:main Sep 29, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 2, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Oct 2, 2023
flutter/packages@d0e9a0e...e2ac440

2023-10-01 [email protected] Roll Flutter from d3df8f6 to d42313c (4 revisions) (flutter/packages#5044)
2023-10-01 [email protected] [webview_flutter] Adds app facing implementation to override console log (flutter/packages#4705)
2023-09-30 [email protected] Roll Flutter from 57b5c3c to d3df8f6 (24 revisions) (flutter/packages#5043)
2023-09-30 [email protected] [webview_flutter] Add a method for getting the user agent (flutter/packages#4472)
2023-09-29 [email protected] [webview_flutter_android] Fix race condition in flaky test (flutter/packages#5037)
2023-09-29 [email protected] [ci] Wait for LUCI test checkin in `release` (flutter/packages#4911)
2023-09-29 [email protected] [webview_flutter_android][webview_flutter_wkwebview] Adds support for `getUserAgent` for `webview_flutter` platform implementations (flutter/packages#4927)
2023-09-29 [email protected] [ci] Disable maps tests in Android emulator (flutter/packages#5003)
2023-09-29 [email protected] [camera] Dispose resources correctly on setDescription (flutter/packages#4003)
2023-09-29 [email protected] [webview_flutter_android] Adds Android implementation to override console log (flutter/packages#4702)
2023-09-29 [email protected] [camera] Remove `@throw` from iOS implementation (flutter/packages#5034)
2023-09-29 [email protected] Roll Flutter from ff4a0f6 to 57b5c3c (47 revisions) (flutter/packages#5036)

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],[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://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Mairramer pushed a commit to Mairramer/flutter that referenced this pull request Oct 10, 2023
flutter/packages@d0e9a0e...e2ac440

2023-10-01 [email protected] Roll Flutter from d3df8f6 to d42313c (4 revisions) (flutter/packages#5044)
2023-10-01 [email protected] [webview_flutter] Adds app facing implementation to override console log (flutter/packages#4705)
2023-09-30 [email protected] Roll Flutter from 57b5c3c to d3df8f6 (24 revisions) (flutter/packages#5043)
2023-09-30 [email protected] [webview_flutter] Add a method for getting the user agent (flutter/packages#4472)
2023-09-29 [email protected] [webview_flutter_android] Fix race condition in flaky test (flutter/packages#5037)
2023-09-29 [email protected] [ci] Wait for LUCI test checkin in `release` (flutter/packages#4911)
2023-09-29 [email protected] [webview_flutter_android][webview_flutter_wkwebview] Adds support for `getUserAgent` for `webview_flutter` platform implementations (flutter/packages#4927)
2023-09-29 [email protected] [ci] Disable maps tests in Android emulator (flutter/packages#5003)
2023-09-29 [email protected] [camera] Dispose resources correctly on setDescription (flutter/packages#4003)
2023-09-29 [email protected] [webview_flutter_android] Adds Android implementation to override console log (flutter/packages#4702)
2023-09-29 [email protected] [camera] Remove `@throw` from iOS implementation (flutter/packages#5034)
2023-09-29 [email protected] Roll Flutter from ff4a0f6 to 57b5c3c (47 revisions) (flutter/packages#5036)

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],[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://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants