firebase / flutterfire

πŸ”₯ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.68k stars 3.97k forks source link

πŸ› [firebase_core] Firebase taking too long to reconnect after returning from background #6355

Closed vcerepin closed 3 years ago

vcerepin commented 3 years ago

Bug report

Description

If an application is in the background for alonger period of time, Firebase will take too long (up to 90 seconds) to establish a connection when returned to the foreground. It seems this happens because of Android's "Doze" mode. This bug is mentioned in many places, including here, but the fix suggested (FirebaseSDKVersion=28.1.0 update) does not solve the issue for me - so maybe this is a different bug.

Steps to reproduce

  1. Open any Flutter app that uses Firebase and run it in debug mode
  2. While the app is in the foreground, lock the screen
  3. Enter command adb shell dumpsys battery unplug
  4. Enter command adb shell dumpsys deviceidle force-idle
  5. After 5-30 minutes, the application will start to log: [{0}] Failed to resolve name. status={1} every few seconds
  6. Unlock your phone, and perform a Firestore request for data that isn't stored in offline persistence

Expected behavior

Expected behavior: Connection is established under 10 seconds Actual behavior: Connection is established under 90 seconds

Sample project

I'm working on it. In the meantime here is a sample project someone else created for the (assumingly) same issue.


Additional context

The issue occurs on both mobile data and Wi-Fi, and in both debug and release mode. I've also tried connecting in my office through a different ISP, and nothing changed.

Devices: 2x Samsung Galaxy A41, Samsung Galaxy A6, iPhone 7 (so far the issue was not detected on iOS, but no proper testing has been done).


Flutter doctor

Click To Expand ``` Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 2.0.6, on Microsoft Windows [Version 6.1.7601], locale hr-HR) [√] Android toolchain - develop for Android devices (Android SDK version 30.0.3) [√] Chrome - develop for the web [√] Android Studio (version 4.0) [√] IntelliJ IDEA Community Edition (version 2020.2) [√] Connected device (2 available) β€’ No issues found! ```

Flutter dependencies

Click To Expand ``` Dart SDK 2.12.3 Flutter SDK 2.0.6 dependencies: - audioplayers 0.19.0 [uuid path_provider http flutter flutter_web_plugins] - cached_video_player 1.0.3 [flutter] - cloud_firestore 2.2.1 [cloud_firestore_platform_interface cloud_firestore_web firebase_core firebase_core_platform_interface flutter meta] - crypto 3.0.1 [collection typed_data] - firebase_auth 1.3.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 1.2.1 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_storage 8.1.1 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] - google_sign_in 4.5.9 [google_sign_in_platform_interface flutter meta google_sign_in_web] - path_provider 2.0.2 [flutter path_provider_platform_interface path_provider_macos path_provider_linux path_provider_windows] - plugin_platform_interface 2.0.0 [meta] - provider 4.3.3 [collection flutter nested] - purchases_flutter 3.2.2 [flutter collection] - shared_preferences 2.0.6 [meta flutter shared_preferences_platform_interface shared_preferences_linux shared_preferences_macos shared_preferences_web shared_preferences_windows] - sign_in_with_apple 2.5.4 [meta flutter] - url_launcher 6.0.6 [flutter meta url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - url_launcher_platform_interface 2.0.3 [flutter plugin_platform_interface] - video_player 1.0.1 [meta video_player_platform_interface video_player_web flutter] - wakelock 0.2.1+1 [flutter meta wakelock_platform_interface wakelock_web] dev dependencies: - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_ scanner term_glyph typed_data] dependency overrides: - cupertino_icons 0.1.3 - firebase_core 1.2.1 [firebase_core_platform_interface firebase_core_web flutter meta] transitive dependencies: - async 2.5.0 [collection] - boolean_selector 2.1.0 [source_span string_scanner] - characters 1.1.0 - charcode 1.2.0 - clock 1.1.0 - cloud_firestore_platform_interface 5.1.1 [collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 2.1.1 [cloud_firestore_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - collection 1.15.0 - csslib 0.16.2 [source_span] - fake_async 1.2.0 [clock collection] - ffi 1.1.2 - file 6.1.1 [meta path] - firebase_auth_platform_interface 4.2.4 [firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 1.2.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.0.1 [collection flutter meta plugin_platform_interface] - firebase_core_web 1.1.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_storage_platform_interface 2.1.1 [collection firebase_core flutter meta plugin_platform_interface] - firebase_storage_web 1.1.1 [async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta] - flutter_web_plugins 0.0.0 [flutter js characters collection meta typed_data vector_math] - google_sign_in_platform_interface 1.1.2 [flutter meta quiver] - google_sign_in_web 0.9.2 [google_sign_in_platform_interface flutter flutter_web_plugins meta js] - html 0.14.0+4 [csslib source_span] - http 0.13.3 [async http_parser meta path pedantic] - http_parser 4.0.0 [charcode collection source_span string_scanner typed_data] - import_js_library 1.0.2 [flutter html flutter_web_plugins js] - intl 0.17.0 [clock path] - js 0.6.3 - matcher 0.12.10 [stack_trace] - meta 1.3.0 - nested 1.0.0 [flutter] - path 1.8.0 - path_provider_linux 2.0.0 [path xdg_directories path_provider_platform_interface flutter] - path_provider_macos 2.0.0 [flutter] - path_provider_platform_interface 2.0.1 [flutter meta platform plugin_platform_interface] - path_provider_windows 2.0.1 [path_provider_platform_interface meta path flutter ffi win32] - pedantic 1.11.0 - platform 3.0.0 - process 4.2.1 [file path platform] - quiver 2.1.5 [matcher meta] - shared_preferences_linux 2.0.0 [flutter file meta path path_provider_linux shared_preferences_platform_interface] - shared_preferences_macos 2.0.0 [shared_preferences_platform_interface flutter] - shared_preferences_platform_interface 2.0.0 [flutter] - shared_preferences_web 2.0.0 [shared_preferences_platform_interface flutter flutter_web_plugins meta] - shared_preferences_windows 2.0.0 [shared_preferences_platform_interface flutter file meta path path_provider_platform_interface path_provider_windows] - sky_engine 0.0.99 - source_span 1.8.0 [charcode collection path term_glyph] - stack_trace 1.10.0 [path] - stream_channel 2.1.0 [async] - string_scanner 1.1.0 [charcode source_span] - term_glyph 1.2.0 - test_api 0.2.19 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher] - typed_data 1.3.0 [collection] - url_launcher_linux 2.0.0 [flutter] - url_launcher_macos 2.0.0 [flutter] - url_launcher_web 2.0.1 [flutter flutter_web_plugins meta url_launcher_platform_interface] - url_launcher_windows 2.0.0 [flutter] - uuid 3.0.4 [crypto] - vector_math 2.1.0 - video_player_platform_interface 2.2.0 [flutter meta] - video_player_web 0.1.4+1 [flutter flutter_web_plugins meta video_player_platform_interface] - wakelock_platform_interface 0.1.0+1 [flutter meta] - wakelock_web 0.1.0+3 [flutter flutter_web_plugins import_js_library js wakelock_platform_interface] - win32 2.0.5 [ffi] - xdg_directories 0.2.0 [meta path process] ```

Exception

Click To Expand ``` W/my.appname(14214): Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed) I/flutter (14214): MyDatabase | getData | [cloud_firestore/unavailable] The service is currently unavailable. This is a most likely a transient condition and may be corrected by retrying with a backoff. W/Firestore(14214): (22.0.1) [WatchStream]: (72436f2) Stream closed with status: Status{code=UNAVAILABLE, description=Unable to resolve host firestore.googleapis.com, cause=java.lang.RuntimeException: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname W/Firestore(14214): at io.grpc.internal.DnsNameResolver.resolveAll(DnsNameResolver.java:436) W/Firestore(14214): at io.grpc.internal.DnsNameResolver$Resolve.resolveInternal(DnsNameResolver.java:272) W/Firestore(14214): at io.grpc.internal.DnsNameResolver$Resolve.run(DnsNameResolver.java:228) W/Firestore(14214): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) W/Firestore(14214): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) W/Firestore(14214): at java.lang.Thread.run(Thread.java:919) W/Firestore(14214): Caused by: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname W/Firestore(14214): at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:156) W/Firestore(14214): at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103) W/Firestore(14214): at java.net.InetAddress.getAllByName(InetAddress.java:1152) W/Firestore(14214): at io.grpc.internal.DnsNameResolver$JdkAddressResolver.resolveAddress(DnsNameResolver.java:646) W/Firestore(14214): at io.grpc.internal.DnsNameResolver.resolveAll(DnsNameResolver.java:404) W/Firestore(14214): ... 5 more W/Firestore(14214): Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname) W/Firestore(14214): at libcore.io.Linux.android_getaddrinfo(Native Method) W/Firestore(14214): at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74) W/Firestore(14214): at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:200) W/Firestore(14214): at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:74) W/Firestore(14214): at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135) W/Firestore(14214): ... 9 more W/Firestore(14214): }. ```

vcerepin commented 3 years ago

After some more adequate testing on iPhone 7, it seems the issue does not occur on iOS.

vcerepin commented 3 years ago

Updated dependencies to the latest versions (that are supposed to remove the backoff problem):

cloud_firestore: ^2.2.2
firebase_storage: ^8.1.3
firebase_core: ^1.3.0
firebase_auth: ^1.4.1
crypto: ^3.0.1

however, the problem still persists.

@tw-tjd @frankyvij - any comments would be greatly appreciated. I may be missing something.

tw-tjd commented 3 years ago

Can you update your firebase_core to 1.3.0. and try again. Your pubspec should have pulled the correct version but we specified it.

vcerepin commented 3 years ago

Please excuse me, it seems the file didn't save upon exiting Android Studio. I have updated firebase_core to 1.3.0, and I can still reproduce the issue 8 out of 10 times. 2 out of 10 times, data is retrieved on the second or third retry (3-5 seconds total).

I have run flutter clean, and even cleared the cache manually before testing.

frankyvij commented 3 years ago

Personally, for me, the issue has not appeared again. There have been a couple of times when my app couldn't connect to the internet immediately (after opening the app from the background), but it definitely did establish the connection in less than 10 seconds.

May I ask if you execute firestore query immediately after coming to the foreground or do you have to press a button to execute a query? I execute the query as soon as the app comes to the foreground with a delay of 500ms, and honestly, I haven't experienced this issue with the latest Firebase Android SDK 28.1.0.

tommienu commented 3 years ago

I'm unfortunately still able to reprod this issue on 1.3.0 when coming back from doze.

Reprod:

  1. Open active Stream in the app
  2. Put the app into the background
  3. Turn off screen (possibly optional)
  4. Put device into deep sleep / doze via ADB (dumpsys battery unplug && dumpsys deviceidle step deep && dumpsys deviceidle get deep)
  5. Confirm that doze is active dumpsys deviceidle get deep > Stepped to deep: IDLE_MAINTENANCE
  6. Re-open the app, the Stream will take a long time to recover. Sometimes indefinitely until the application is killed and sometimes it recovers after about 1 minute.

Logcat is reporting that flutter cannot reach the dns-entry for firebase. But I can confirm (by switching to another Internet-based app, such as the browser) that the connectivity on the device has recovered properly.

If "feels" like 1.3.0 has improved regular resume from the background, meaning returning from non-doze background. But we might have another issue with returning from doze.

Logcat:

V/NativeCrypto(26604): Read error: ssl=0x725860ead8: I/O error during system call, Software caused connection abort
V/NativeCrypto(26604): Write error: ssl=0x725860ead8: I/O error during system call, Broken pipe
V/NativeCrypto(26604): SSL shutdown failed: ssl=0x725860ead8: I/O error during system call, Success
W/Firestore(26604): (23.0.1) [WatchStream]: (ac20d5e) Stream closed with status: Status{code=UNAVAILABLE, description=End of stream or IOException, cause=null}.
W/ManagedChannelImpl(26604): [{0}] Failed to resolve name. status={1}
W/Firestore(26604): (23.0.1) [WatchStream]: (ac20d5e) Stream closed with status: Status{code=UNAVAILABLE, description=Unable to resolve host firestore.googleapis.com, cause=java.lang.RuntimeException: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname

Screenshot of my flutter app while is this state (the live games are a Stream and the upcoming games are from cache): image

Screenshot of a browser confirming that internet on the device is working while the Flutter app is in this state: image

markusaksli-nc commented 3 years ago

I was not able to reproduce this issue as well with

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^1.3.0
  cloud_firestore: ^2.2.2

Seeing as no others have run into this issue either I'm going to close this issue for now as it needs a generally reproducible case on the latest versions. Please make sure you aren't overriding the Firebase SDK version in your app-level build.gradle and try flutter clean or deleting gradle cache.

vcerepin commented 3 years ago

@frankyvij - No, I have a button that leads to a page which retrieves data through a FutureBuilder. Which means at least 1500ms before the first execution.

@markusaksli-nc - At the moment I'm using sdk: ">=2.7.0 <3.0.0", so before I refactor my entire application for null-safety; Can this be the cause of the issue? Also, when you were testing, did you query immediately after adb shell dumpsys deviceidle force-idle, or did you wait at least 10 minutes?

markusaksli-nc commented 3 years ago

I waited form more than 10 minutes as with the previous iteration of this issue. I was on nullsafety but I don't really see how that could cause the issue for you (however you are on an outdated flutter version, latest stable is 2.2.2)