Skip to content

Conversation

@flutteractionsbot
Copy link

@flutteractionsbot flutteractionsbot commented Oct 16, 2025

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 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?

  • Low
  • Medium
  • High

Test Coverage:

Are you confident that your fix is well-tested by automated tests?

  • Yes
  • No

Validation Steps:

What are the steps to validate that this fix works?

Follow the steps in the issue description, 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.

… 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
@flutteractionsbot flutteractionsbot added the cp: review Cherry-picks in the review queue label Oct 16, 2025
@flutteractionsbot flutteractionsbot requested a review from a team as a code owner October 16, 2025 18:11
@flutteractionsbot flutteractionsbot added the cp: review Cherry-picks in the review queue label Oct 16, 2025
@flutteractionsbot
Copy link
Author

@camsim99 please fill out the PR description above, afterwards the release team will review this request.

@flutter-dashboard
Copy link

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.

@github-actions github-actions bot added platform-android Android applications specifically engine flutter/engine related. See also e: labels. team-android Owned by Android platform team labels Oct 16, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines +106 to +107
* <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.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
* <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.

Comment on lines 1619 to 1620
FlutterActivityAndFragmentDelegate delegate =
spy(new FlutterActivityAndFragmentDelegate(mockHost));
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
FlutterActivityAndFragmentDelegate delegate =
spy(new FlutterActivityAndFragmentDelegate(mockHost));
FlutterActivityAndFragmentDelegate delegate =
new FlutterActivityAndFragmentDelegate(mockHost);

Copy link
Contributor

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.

Copy link
Contributor

@reidbaker reidbaker left a comment

Choose a reason for hiding this comment

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

RSLGTM

@reidbaker reidbaker added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 21, 2025
@auto-submit auto-submit bot merged commit 0353165 into flutter:flutter-3.35-candidate.0 Oct 21, 2025
160 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 23, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 23, 2025
auto-submit bot pushed a commit that referenced this pull request Oct 23, 2025
…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.
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 24, 2025
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 cp: review Cherry-picks in the review queue engine flutter/engine related. See also e: labels. platform-android Android applications specifically team-android Owned by Android platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants