Skip to content

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

Merged
auto-submit[bot] merged 13 commits into
flutter:mainfrom
bparrishMines:manager_early_finalize
May 15, 2025
Merged

auto-submit[bot] merged 13 commits into
flutter:mainfrom
bparrishMines:manager_early_finalize

Conversation

@bparrishMines

@bparrishMines bparrishMines commented May 8, 2025 •

Copy link
Copy Markdown
Contributor
  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
- io
- js
- json_serializable
- leak_tracker

Copy link
Copy Markdown
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

@tarrinneal tarrinneal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

little nit

///
/// Returns the randomly generated id of the [instance] added.
int addDartCreatedInstance($proxyApiBaseClassName instance) {
assert(getIdentifier(instance) == null);

Copy link
Copy Markdown

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

@bparrishMines bparrishMines May 13, 2025 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown

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')

@stuartmorgan-g stuartmorgan-g left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@bparrishMines

Copy link
Copy Markdown
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
@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 [email protected] [pigeon]
Prevents premature finalization from unused Dart instance for ProxyApis
(flutter/packages#9231)
2025-05-14 [email protected] [in_app_purchase] Bump
`in_app_purchase_storekit` version (flutter/packages#9247)
2025-05-14 [email protected] Roll Flutter from
336a7ec to 0b9f928 (22 revisions) (flutter/packages#9251)
2025-05-14 [email protected]
[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 [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://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.
Ortes pushed a commit to Ortes/packages that referenced this pull request Jun 25, 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.
creatorpiyush pushed a commit to creatorpiyush/packages that referenced this pull request Jun 10, 2026
…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.
bisor0627 pushed a commit to bisor0627/packages that referenced this pull request Jun 19, 2026
…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.
Nakiha pushed a commit to Nakiha/VoidPlayer-Flutter that referenced this pull request Sep 28, 2026
flutter/packages@1468581...2dff621

2025-05-15 [email protected] [pigeon]
Prevents premature finalization from unused Dart instance for ProxyApis
(flutter/packages#9231)
2025-05-14 [email protected] [in_app_purchase] Bump
`in_app_purchase_storekit` version (flutter/packages#9247)
2025-05-14 [email protected] Roll Flutter from
470f481 to 5557d57 (22 revisions) (flutter/packages#9251)
2025-05-14 [email protected]
[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 [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://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
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