Skip to content

[pigeon] Prevents premature finalization from unused Dart instance for ProxyApis #9231

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
May 15, 2025

Conversation

bparrishMines
Copy link
Contributor

@bparrishMines bparrishMines commented May 8, 2025

  1. The Dart InstanceManager.addHostCreatedInstance is only ever used to respond to native calls that create a new instance right before the instance is returned in a host method call or passed in a flutter method call. So, addHostCreatedInstance now only adds a strong referenced instance and not the weak referenced instance. This will ensure the weak referenced instance is not created until it is being passed to a user immediately.
  2. The Dart InstanceManager.remove now asserts that the strong referenced instance isn't removed before the weak referenced instance. This should prevent future scenarios where the user is holding a weak referenced instance while the native side no longer has any reference.

Fixes flutter/flutter#168531

Pre-Review Checklist

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

@bparrishMines bparrishMines changed the title Manager early finalize [pigeon] Prevents premature finalization from unused Dart instance for ProxyApis May 8, 2025
@bparrishMines bparrishMines marked this pull request as ready for review May 9, 2025 18:34
@bparrishMines bparrishMines requested a review from tarrinneal as a code owner May 9, 2025 18:34
@@ -41,6 +41,7 @@
- io
- js
- json_serializable
- leak_tracker
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added to use forceGC

Copy link
Contributor

@tarrinneal tarrinneal left a comment

Choose a reason for hiding this comment

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

little nit

@@ -110,8 +110,17 @@ class $dartInstanceManagerClassName {
///
/// Returns the randomly generated id of the [instance] added.
int addDartCreatedInstance($proxyApiBaseClassName instance) {
assert(getIdentifier(instance) == null);

Choose a reason for hiding this comment

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

small feedback: adding a msg to this assert

Copy link
Contributor Author

@bparrishMines bparrishMines May 13, 2025

Choose a reason for hiding this comment

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

It is usually my preference that adding a message is dependent on whether the assertion is self explanatory to the person who uses the method. InstanceManager.addDartCreatedInstance should only ever be called by the generated code, so I assume a maintainer of pigeon would understand this error without a message. While a user of pigeon would not get much value from an error message that states the InstanceManager already contains the instance.

But I'm biased since I wrote this, so I could be convinced if the intention of the check is not immediately clear to a new maintainer.

@jointhejourney
Copy link

I'm testing this a bit, and noticed that any proxy api class that has a @static property is giving me this assertion error when doing a hot restart:

_AssertionError (Failed assertion: line 258 pos 7: 'instance == null': A strong instance with identifier 0 is being removed despite the weak reference still existing: Instance of 'JourneyProvider')

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
Copy link
Contributor Author

@jointhejourney Thanks for catching this. I added a fix that detaches the finalizers when the Swift InstanceManager.removeAllObjects is called.

@bparrishMines bparrishMines added the autosubmit Merge PR when tree becomes green via auto submit App label May 15, 2025
@auto-submit auto-submit bot merged commit 2dff621 into flutter:main May 15, 2025
82 checks passed
@bparrishMines bparrishMines deleted the manager_early_finalize branch May 15, 2025 00:59
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 15, 2025
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request May 15, 2025
flutter/packages@1468581...2dff621

2025-05-15 10687576+bparrishMines@users.noreply.github.com [pigeon]
Prevents premature finalization from unused Dart instance for ProxyApis
(flutter/packages#9231)
2025-05-14 louisehsu@google.com [in_app_purchase] Bump
`in_app_purchase_storekit` version (flutter/packages#9247)
2025-05-14 engine-flutter-autoroll@skia.org Roll Flutter from
336a7ec to 0b9f928 (22 revisions) (flutter/packages#9251)
2025-05-14 10687576+bparrishMines@users.noreply.github.com
[webview_flutter_android][webview_flutter_wkwebview] Adds support to set
whether to draw the scrollbar (flutter/packages#9249)

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 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
FMorschel pushed a commit to FMorschel/packages that referenced this pull request Jun 9, 2025
…r ProxyApis (flutter#9231)

1. The Dart `InstanceManager.addHostCreatedInstance` is only ever used to respond to native calls that create a new instance right before the instance is returned in a host method call or passed in a flutter method call. So, `addHostCreatedInstance` now only adds a strong referenced instance and not the weak referenced instance. This will ensure the weak referenced instance is not created until it is being passed to a user immediately.
2. The Dart `InstanceManager.remove` now asserts that the strong referenced instance isn't removed before the weak referenced instance. This should prevent future scenarios where the user is holding a weak referenced instance while the native side no longer has any reference.

Fixes flutter/flutter#168531

## 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.
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: pigeon platform-android platform-ios platform-macos
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pigeon's ProxyApi causes crash on iOS when multiple instances are created in parallel
4 participants