-
Notifications
You must be signed in to change notification settings - Fork 29.7k
[CP-stable][Android] Refactor ImageReaderSurfaceProducer restoration after app resumes
#177112
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
[CP-stable][Android] Refactor ImageReaderSurfaceProducer restoration after app resumes
#177112
Conversation
… resumes (flutter#175937) Refactors `FlutterRenderer` callback on app resume to restore its `ImageReaderSurfaceProducer`s to a public API that Flutter activities and fragments can call themselves `onResume` to fix a memory leak caused by a potentially infinite number of callbacks being created as new instances of the `FlutterEngine` are created. Fixes flutter#173770. Directly follows the @/jason-simmons recommendation in flutter#173770 (comment). ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
|
@camsim99 please fill out the PR description above, afterwards the release team will review this request. |
|
This pull request was opened from and to a release candidate branch. This should only be done as part of the official Flutter release process. If you are attempting to make a regular contribution to the Flutter project, please close this PR and follow the instructions at Tree Hygiene for detailed instructions on contributing to Flutter. Reviewers: Use caution before merging pull requests to release branches. Ensure the proper procedure has been followed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request refactors the restoration logic for ImageReaderSurfaceProducer on Android. It decouples FlutterRenderer from Android's ProcessLifecycleOwner by moving the responsibility of triggering the restoration to FlutterActivityAndFragmentDelegate's onResume method. This is a good architectural improvement that makes the logic more explicit and improves separation of concerns. The changes are well-tested with updates to existing test files. My review includes a couple of minor suggestions to improve documentation clarity and simplify a test case.
| * <p>All {@code FlutterActivity}s and {@code FlutterFragment}s are expected to call this {@code | ||
| * onResume} to ensure surface producers are restored when the app returns to the foreground. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment here is a bit confusing. It says "...are expected to call this {@code onResume}...", which could be misinterpreted. To improve clarity, I suggest rephrasing it to specify that this method should be called from within the onResume() implementation of the host component.
| * <p>All {@code FlutterActivity}s and {@code FlutterFragment}s are expected to call this {@code | |
| * onResume} to ensure surface producers are restored when the app returns to the foreground. | |
| * <p>All {@code FlutterActivity}s and {@code FlutterFragment}s are expected to call this method | |
| * in their {@code onResume()} implementation to ensure surface producers are restored when the app returns to the foreground. |
| FlutterActivityAndFragmentDelegate delegate = | ||
| spy(new FlutterActivityAndFragmentDelegate(mockHost)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of spy() here is unnecessary because none of the methods on the delegate object are being stubbed for this test. Creating a direct instance with new FlutterActivityAndFragmentDelegate(mockHost) is sufficient and makes the test's intent clearer.
| FlutterActivityAndFragmentDelegate delegate = | |
| spy(new FlutterActivityAndFragmentDelegate(mockHost)); | |
| FlutterActivityAndFragmentDelegate delegate = | |
| new FlutterActivityAndFragmentDelegate(mockHost); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a good catch so corrected this here and put up #177120 to fix in master. I will make sure the spy doesn't make it in my 3.38 beta cherry pick either.
reidbaker
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RSLGTM
0353165
into
flutter:flutter-3.35-candidate.0
…restoration after app resumes (flutter/flutter#177112)
…restoration after app resumes (flutter/flutter#177112)
…after app resumes (#177121) > [!NOTE] > To release engineer: this can definitely wait to be included in a hotfix beta release. I also want to CP this to stable: #177112 ### Issue Link: What is the link to the issue this cherry-pick is addressing? #173770 ### Changelog Description: Explain this cherry pick in one line that is accessible to most Flutter developers. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples Mitigates a memory leak that occurs on Android, when `Activities` are not kept upon exit and an Activity is exited and re-entered. ### Impact Description: What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch) Memory leak that will occur in production app but does not impact development. ### Workaround: Is there a workaround for this issue? Not that I know of. ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: What are the steps to validate that this fix works? Follow the steps in the [issue description](#173770 (comment)), roughly: 1. Create a Flutter project. 2. Enter the developer options, set not to keep activities, (destroy each activity as soon as the user leaves it) 3. Entering the activity, clicking the home button, exiting the activity, and then entering the activity again will trigger the destruction and reconstruction of the activity. 4. Check memory leaks through profiler and find memory leaks. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.
…restoration after app resumes (flutter/flutter#177112)
Note
To release engineer: this can definitely wait to be included in a hotfix beta release. I also want to CP this to 3.38 beta as part of a hotfix release: #177121
This pull request is created by automatic cherry pick workflow
Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request.
Issue Link:
What is the link to the issue this cherry-pick is addressing?
#173770
Changelog Description:
Explain this cherry pick in one line that is accessible to most Flutter developers. See best practices for examples
Mitigates a memory leak that occurs on Android, when
Activitiesare not kept upon exit and an Activity is exited and re-entered.Impact Description:
What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch)
Memory leak that will occur in production app but does not impact development.
Workaround:
Is there a workaround for this issue?
Not that I know of.
Risk:
What is the risk level of this cherry-pick?
Test Coverage:
Are you confident that your fix is well-tested by automated tests?
Validation Steps:
What are the steps to validate that this fix works?
Follow the steps in the issue description, roughly: