shorebirdtech / shorebird

Code Push for Flutter and other tools for Flutter businesses.
https://shorebird.dev
Other
2.32k stars 140 forks source link

fix: ANR when using Firebase Cloud Messaging with Shorebird #695

Closed eseidel closed 7 months ago

eseidel commented 1 year ago

Report from a customer: image

Definitely looks like our bug.

There are at least two bugs I could imagine here:

  1. Launching a second Flutter engine (if it goes though flutter_main) could end up blocked on our global lock for rust config if the background thread is still doing the update download.
  2. If it's trying to launch with something other than libapp.so, we might end up launching from the wrong Dart bundle (we should check that they're targeting libapp.so).

In either case, we need to make an example app which uses FlutterFirebaseMessagingBackgroundExecutor and see if we can reproduce: https://github.com/firebase/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/android/src/main/java/io/flutter/plugins/firebase/messaging/FlutterFirebaseMessagingBackgroundExecutor.java

bryanoltman commented 1 year ago

I set up a test project with FCM and was able to receive and open background notifications without issue. One thing I'm noticing from the crash log is the split_config.arm6... in the left column. Maybe FCM is a red herring here?

bryanoltman commented 1 year ago

Going through the steps of adding crashlytics to our demo app and noticed the bit in https://firebase.google.com/docs/crashlytics/get-started?platform=flutter#add-sdk about "If your Flutter project uses the --split-debug-info flag"

eseidel commented 1 year ago

Several issues here, starting to catalogue them

  1. Firebase will end up causing Flutter to call nativeInit twice. We don't expect that, and won't want to init twice.
  2. However we will want any second-init to use the current boot patch (and not have to wait on a lock to do so).
eseidel commented 1 year ago

We believe we understand the issue now.

The hang is believed non-fatal (the app doesn't crash, or hang indefinitely), but will hang (in the background) for some period of time thus triggering ANR reports from Android/Crashlytics. We believe it occurs when:

  1. FCM triggers in the background.
  2. Shorebird is being used in the app.
  3. Multiple FCM messages are queued.
  4. The network is slow/unreliable.

We have a fix planned and being worked on. It's a slightly involved refactoring of our Rust code which will likely take most of today but should remove this entire class of issues. I expect us to release a fix tomorrow. We also have a (planned) release coming out today, (0.5.1) which will not have this fix in it yet.

Thank you for your patience.

eseidel commented 1 year ago

Fixes planned:

bryanoltman commented 1 year ago

Fix Engine code to not call into Shorebird when not using libapp.so and log a warning. This is a speculative fix noticed while in this code. Although FCM is still using libapp.so, other callers of Flutter might not, and it appears we will do the wrong thing for them, so might as well fix it while we're there.

Addressed in https://github.com/shorebirdtech/engine/pull/31

eseidel commented 1 year ago

I'm pleased to report that I have a fix compiling for 2. and 3, testing now.

linhmanh92 commented 1 year ago

I am the one who reported this error to you on discord. glad that the fix is still going well. Thank you.