Skip to content

[webview_flutter_android] [camera_android_camerax] Updates internal Java InstanceManager to only stop finalization callbacks when stopped #3571

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 11 commits into from
Apr 14, 2023

Conversation

bparrishMines
Copy link
Contributor

@bparrishMines bparrishMines commented Mar 28, 2023

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.

@bparrishMines bparrishMines requested a review from camsim99 as a code owner March 28, 2023 16:36
@bparrishMines bparrishMines changed the title Instance manager update [webview_flutter_android] [camera_android_camerax] Updates internal Java Mar 28, 2023
@bparrishMines bparrishMines changed the title [webview_flutter_android] [camera_android_camerax] Updates internal Java [webview_flutter_android] [camera_android_camerax] Updates internal Java InstanceManager to only stop finalization callbacks when stopped Mar 28, 2023
handler.removeCallbacks(this::releaseAllFinalizedInstances);
isClosed = true;
clear();
hasFinalizationListenerStopped = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to make sure I understand correctly, can you confirm the following is correct?

After this method called, objects that have been added to this instance manager can be garbage collected. Thus, objects might be contained by the instance manager (because we aren't releasing the resources/calling clear() anymore), but also might not. So basically, the instance manager is now unreliable, but with this change, it's up the developer to be aware of that versus getting a null, -1, etc. value back when attempting to call a method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When a new instance is added to the manager, the manager creates a strong reference and a weak reference to the instance. When remove(Object) is called, the strong reference is removed. Therefore, the weak reference to the instance will remain until it is garbage collected. The manager now functions this way whether this value is true or false. The only difference now is that the finalizationListener that was passed in InstanceManager.create is no longer called when the weak reference to an instance is claimed by garbage collection.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see. This makes sense!

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to test the case where the weak reference was claimed by garbage collection to see that the finalizationListener is no longer called? I figure it's not but asking just in case!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is possible with the Runtime.getRuntime().gc(); method. I tried to add a unit test, but the Android Handler class is just a stub when running unit tests. So I created an integration test.

@gmackall gmackall requested a review from camsim99 April 13, 2023 18:43
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.

LGTM! Just left a suggestion about a test

handler.removeCallbacks(this::releaseAllFinalizedInstances);
isClosed = true;
clear();
hasFinalizationListenerStopped = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see. This makes sense!

handler.removeCallbacks(this::releaseAllFinalizedInstances);
isClosed = true;
clear();
hasFinalizationListenerStopped = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to test the case where the weak reference was claimed by garbage collection to see that the finalizationListener is no longer called? I figure it's not but asking just in case!

handler.removeCallbacks(this::releaseAllFinalizedInstances);
isClosed = true;
clear();
hasFinalizationListenerStopped = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to derive this value instead of using a boolean?
For example if handler.removeCallbacks does not have any more callbacks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like there is a Handler.hasCallbacks, but it requires Android 29+ unfortunately.

@bparrishMines bparrishMines added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 14, 2023
@auto-submit auto-submit bot merged commit 44207e7 into flutter:main Apr 14, 2023
@bparrishMines bparrishMines deleted the instance_manager_update branch April 14, 2023 20:57
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 17, 2023
…ternal Java InstanceManager to only stop finalization callbacks when stopped (flutter/packages#3571)
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Apr 17, 2023
flutter/packages@e4ec155...0277f2a

2023-04-16 engine-flutter-autoroll@skia.org Roll Flutter from 00171b0 to 50171bb (7 revisions) (flutter/packages#3723)
2023-04-15 engine-flutter-autoroll@skia.org Roll Flutter from f740544 to 00171b0 (17 revisions) (flutter/packages#3717)
2023-04-15 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 2.2.9 to 2.2.12 (flutter/packages#3711)
2023-04-15 49699333+dependabot[bot]@users.noreply.github.com Bump actions/checkout from 3.5.0 to 3.5.2 (flutter/packages#3710)
2023-04-14 stuartmorgan@google.com [path_provider] Fix Android lint warnings (flutter/packages#3706)
2023-04-14 10687576+bparrishMines@users.noreply.github.com [webview_flutter_android] [camera_android_camerax] Updates internal Java InstanceManager to only stop finalization callbacks when stopped (flutter/packages#3571)
2023-04-14 stuartmorgan@google.com [shared_preferences] Fix Android Java version issue (flutter/packages#3712)
2023-04-14 48185017+andreisas06@users.noreply.github.com [image_picker][android] Non-bitmap images now return path instead of null (flutter/packages#3590)
2023-04-14 engine-flutter-autoroll@skia.org Roll Flutter from be45eb2 to f740544 (24 revisions) (flutter/packages#3713)

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 flutter-ecosystem@google.com,rmistry@google.com 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
nploi pushed a commit to nploi/packages that referenced this pull request Jul 16, 2023
…ava InstanceManager to only stop finalization callbacks when stopped (flutter#3571)
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 p: webview_flutter platform-android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants