Skip to content

Assert that runApp is called in the same zone as binding.ensureInitialized #122836

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 1 commit into from
Mar 16, 2023

Conversation

Hixie
Copy link
Contributor

@Hixie Hixie commented Mar 16, 2023

Fixes #94123

@flutter-dashboard flutter-dashboard bot added a: tests "flutter test", flutter_test, or one of our tests framework flutter/packages/flutter repository. See also f: labels. c: contributor-productivity Team-specific productivity, code health, technical debt. labels Mar 16, 2023
@Hixie
Copy link
Contributor Author

Hixie commented Mar 16, 2023

Previously landed as #117113.
Previously reverted as #122830.

Copy link
Contributor

@christopherfujino christopherfujino left a comment

Choose a reason for hiding this comment

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

LGTM

@christopherfujino
Copy link
Contributor

For posterity, the failures this was reverted for were actually fixed in 6711411

@Hixie Hixie added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 16, 2023
@auto-submit auto-submit bot merged commit 245d6d4 into flutter:master Mar 16, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 10, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 10, 2023
@mit-mit
Copy link
Member

mit-mit commented May 12, 2023

@Hixie a few folks have requested that should be documented with migration steps as a breaking change.

@gabrimatic
Copy link

@Hixie a few folks have requested that should be documented with migration steps as a breaking change.

@mit-mit You're true! I am getting this error after updating to Flutter 3.10:

════════ Exception caught by Flutter framework ══════════════════
The following assertion was thrown during runApp:
Zone mismatch.

The Flutter bindings were initialized in a different zone than is now being used. This will likely cause confusion and bugs as any zone-specific configuration will inconsistently use the configuration of the original binding initialization zone or this zone based on hard-to-predict factors such as which zone was active when a particular callback was set.
It is important to use the same zone when calling `ensureInitialized` on the binding as when calling `runApp` later.
To make this warning fatal, set BindingBase.debugZoneErrorsAreFatal to true before the bindings are initialized (i.e. as the first statement in `void main() { }`).
When the exception was thrown, this was the stack
#0      BindingBase.debugCheckZone.<anonymous closure>
binding.dart:497
#1      BindingBase.debugCheckZone
binding.dart:502
#2      runApp
binding.dart:1080
#3      main.<anonymous closure>.<anonymous closure>
main.dart:60
<asynchronous suspension>

@Hixie
Copy link
Contributor Author

Hixie commented May 12, 2023

Sure, I'll see what I can whip up.

@Hixie Hixie deleted the zones branch May 12, 2023 21:02
Hixie added a commit to flutter/website that referenced this pull request May 13, 2023
@Hixie
Copy link
Contributor Author

Hixie commented May 13, 2023

proposed migration guide: flutter/website#8683

parlough added a commit to flutter/website that referenced this pull request May 16, 2023
@filiph
Copy link
Contributor

filiph commented Jun 4, 2023

Hi @Hixie, I need guidance on another use of Zone than is not mentioned in your migration guide.

Historically, when using Firebase Crashlytics, Zones were used to catch all errors: https://firebase.flutter.dev/docs/crashlytics/usage/#zoned-errors — even those in various callbacks. For something like Crashlytics, this is useful, because you do want to have reports about all your errors.

I used this approach in the game_template sample, among other places. But now I see this is wrong through the warning discussed here (flutter/samples#1818).

I was going to ask if there is any new guidance on cases like this, when you want to run your app in a Zone (i.e. runApp is called from a non-root zone), but you also need to send the errors from that zone to a plugin (i.e. ensureInitialized needs to be called before the zone is created).

Newer Crashlytics documentation does not mention this case at all.

cc @domesticmouse

@Hixie
Copy link
Contributor Author

Hixie commented Jun 5, 2023

Create the zone, call initialize binding in the zone, then configure the plugin, then call runApp. You'll need to do something like have the zone error handler call a configurable callback that you can set up from inside the zone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: tests "flutter test", flutter_test, or one of our tests autosubmit Merge PR when tree becomes green via auto submit App c: contributor-productivity Team-specific productivity, code health, technical debt. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flutter framework does not warn when ensureInitialized is called in a different zone than runApp
5 participants