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.71k stars 3.97k forks source link

[cloud_firestore]: Async transactions never return only on iOS #13336

Closed ClementCardonnel closed 1 month ago

ClementCardonnel commented 1 month ago

Is there an existing issue for this?

Which plugins are affected?

Core, Database

Which platforms are affected?

iOS

Description

The following lines execute correctly on Flutter Android and Web, but don't work on iOS:

final doc = FirebaseFirestore.instance.collection('trips').doc();
await doc.set(firebaseDocument);
return doc.id;

What's happening is doc.set is successfully called (the value is being written on Firestore), but await is never called. It just never returns. Whereas on Android and Web, I'm able to return doc.id.

Reproducing the issue

Using Flutter 3.24.3, firebase_core: ^3.4.1 firebase_auth: ^5.2.1 cloud_firestore: ^5.4.1

And having this line in my Podfile: pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '11.0.0' Update: I've also tried it without this line, it didn't change a thing.

I have Firebase Authentication configured and my test user is signed in, the failing requests are authenticated via security rules. Just thought it was important enough to be mentioned.

In my app, I only have to await a FirebaseFirestore.get request for the issue to happen.

I've reproduced it on an iOS 15 iPhone, and an iOS 17 iPad and iPhone.

Firebase Core version

3.4.1

Flutter Version

3.24.3

Relevant Log Output

No response

Flutter dependencies

Expand Flutter dependencies snippet
```yaml Dart SDK 3.5.3 Flutter SDK 3.24.3 leafty 0.5.0 dependencies: - animated_size_and_fade 4.0.0 [flutter] - async 2.11.0 [collection meta] - cloud_firestore 5.3.0 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - collection 1.18.0 - cupertino_icons 1.0.8 - dotted_border 2.1.0 [flutter path_drawing] - dual_screen 1.0.4 [flutter] - easy_debounce 2.0.3 - envied 0.5.4+1 - figma_squircle 0.5.3 [vector_math flutter] - firebase_analytics 11.3.0 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter] - firebase_auth 5.2.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 3.4.1 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 4.1.0 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] - flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math] - flutter_secure_storage 9.2.2 [flutter flutter_secure_storage_linux flutter_secure_storage_macos flutter_secure_storage_platform_interface flutter_secure_storage_web flutter_secure_storage_windows meta] - flutter_svg 2.0.10+1 [flutter http vector_graphics vector_graphics_codec vector_graphics_compiler] - fluttertoast 8.2.8 [flutter flutter_web_plugins web] - geolocator 13.0.1 [flutter geolocator_platform_interface geolocator_android geolocator_apple geolocator_web geolocator_windows] - go_router 14.2.7 [collection flutter flutter_web_plugins logging meta] - google_fonts 6.2.1 [flutter http path_provider crypto] - google_maps_flutter 2.9.0 [flutter google_maps_flutter_android google_maps_flutter_ios google_maps_flutter_platform_interface google_maps_flutter_web] - google_sign_in 6.2.1 [flutter google_sign_in_android google_sign_in_ios google_sign_in_platform_interface google_sign_in_web] - http 1.2.2 [async http_parser meta web] - intl 0.19.0 [clock meta path] - jwt_decoder 2.0.1 - material_design_icons_flutter 7.0.7296 [flutter] - provider 6.1.2 [collection flutter nested] - pull_down_button 0.10.1 [flutter meta] - shared_preferences 2.3.2 [flutter shared_preferences_android shared_preferences_foundation shared_preferences_linux shared_preferences_platform_interface shared_preferences_web shared_preferences_windows] - sprung 3.0.1 [flutter] - url_launcher 6.3.0 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - uuid 4.5.0 [crypto sprintf meta fixnum] dev dependencies: - build_runner 2.4.12 [analyzer args async build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style frontend_server_client glob graphs http_multi_server io js logging meta mime package_config path pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml] - envied_generator 0.5.4+1 [envied build code_builder dart_style source_gen analyzer recase equatable] - flutter_lints 4.0.0 [lints] - flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math leak_tracker_flutter_testing async boolean_selector characters collection leak_tracker leak_tracker_testing material_color_utilities meta source_span stream_channel string_scanner term_glyph vm_service] - mockito 5.4.4 [analyzer build code_builder collection dart_style matcher meta path source_gen test_api] - test 1.25.7 [analyzer async boolean_selector collection coverage http_multi_server io js matcher node_preamble package_config path pool shelf shelf_packages_handler shelf_static shelf_web_socket source_span stack_trace stream_channel test_api test_core typed_data web_socket_channel webkit_inspection_protocol yaml] transitive dependencies: - _fe_analyzer_shared 72.0.0 [meta] - _flutterfire_internals 1.3.42 [collection firebase_core firebase_core_platform_interface flutter meta] - _macros 0.3.2 - analyzer 6.7.0 [_fe_analyzer_shared collection convert crypto glob macros meta package_config path pub_semver source_span watcher yaml] - args 2.5.0 - boolean_selector 2.1.1 [source_span string_scanner] - build 2.4.1 [analyzer async convert crypto glob logging meta package_config path] - build_config 1.1.1 [checked_yaml json_annotation path pubspec_parse yaml] - build_daemon 4.0.2 [built_collection built_value crypto http_multi_server logging path pool shelf shelf_web_socket stream_transform watcher web_socket_channel] - build_resolvers 2.4.2 [analyzer async build collection convert crypto graphs logging package_config path pool pub_semver stream_transform yaml] - build_runner_core 7.3.2 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta package_config path pool timing watcher yaml] - built_collection 5.1.1 - built_value 8.9.2 [built_collection collection fixnum meta] - characters 1.3.0 - checked_yaml 2.0.3 [json_annotation source_span yaml] - clock 1.1.1 - cloud_firestore_platform_interface 6.3.2 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 4.1.2 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins] - code_builder 4.10.0 [built_collection built_value collection matcher meta] - convert 3.1.1 [typed_data] - coverage 1.9.2 [args glob logging package_config path source_maps stack_trace vm_service] - crypto 3.0.5 [typed_data] - csslib 1.0.0 [source_span] - dart_style 2.3.6 [analyzer args collection path pub_semver source_span] - equatable 2.0.5 [collection meta] - fake_async 1.3.1 [clock collection] - ffi 2.1.3 - file 7.0.0 [meta path] - firebase_analytics_platform_interface 4.2.2 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_analytics_web 0.5.9+2 [_flutterfire_internals firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins] - firebase_auth_platform_interface 7.4.5 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 5.13.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser meta web] - firebase_core_platform_interface 5.2.1 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 2.18.0 [firebase_core_platform_interface flutter flutter_web_plugins meta web] - firebase_crashlytics_platform_interface 3.6.41 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - fixnum 1.1.0 - flutter_plugin_android_lifecycle 2.0.22 [flutter] - flutter_secure_storage_linux 1.2.1 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_macos 3.1.2 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_platform_interface 1.1.2 [flutter plugin_platform_interface] - flutter_secure_storage_web 1.2.1 [flutter flutter_secure_storage_platform_interface flutter_web_plugins js] - flutter_secure_storage_windows 3.1.2 [ffi flutter flutter_secure_storage_platform_interface path path_provider win32] - flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math] - frontend_server_client 4.0.0 [async path] - geolocator_android 4.6.1 [flutter geolocator_platform_interface meta uuid] - geolocator_apple 2.3.7 [flutter geolocator_platform_interface] - geolocator_platform_interface 4.2.4 [flutter plugin_platform_interface vector_math meta] - geolocator_web 4.1.1 [flutter flutter_web_plugins geolocator_platform_interface web] - geolocator_windows 0.2.3 [flutter geolocator_platform_interface] - glob 2.1.2 [async collection file path string_scanner] - google_identity_services_web 0.3.1+4 [meta web] - google_maps 8.0.0 [meta web] - google_maps_flutter_android 2.14.4 [flutter flutter_plugin_android_lifecycle google_maps_flutter_platform_interface stream_transform] - google_maps_flutter_ios 2.13.0 [flutter google_maps_flutter_platform_interface stream_transform] - google_maps_flutter_platform_interface 2.9.0 [collection flutter plugin_platform_interface stream_transform] - google_maps_flutter_web 0.5.10 [collection flutter flutter_web_plugins google_maps google_maps_flutter_platform_interface sanitize_html stream_transform web] - google_sign_in_android 6.1.28 [flutter google_sign_in_platform_interface] - google_sign_in_ios 5.7.7 [flutter google_sign_in_platform_interface] - google_sign_in_platform_interface 2.4.5 [flutter plugin_platform_interface] - google_sign_in_web 0.12.4+2 [flutter flutter_web_plugins google_identity_services_web google_sign_in_platform_interface http web] - graphs 2.3.2 [collection] - html 0.15.4 [csslib source_span] - http_multi_server 3.2.1 [async] - http_parser 4.0.2 [collection source_span string_scanner typed_data] - io 1.0.4 [meta path string_scanner] - js 0.6.7 [meta] - json_annotation 4.9.0 [meta] - leak_tracker 10.0.5 [clock collection meta path vm_service] - leak_tracker_flutter_testing 3.0.5 [flutter leak_tracker leak_tracker_testing matcher meta] - leak_tracker_testing 3.0.1 [leak_tracker matcher meta] - lints 4.0.0 - logging 1.2.0 - macros 0.1.2-main.4 [_macros] - matcher 0.12.16+1 [async meta stack_trace term_glyph test_api] - material_color_utilities 0.11.1 [collection] - meta 1.15.0 - mime 1.0.6 - nested 1.0.0 [flutter] - node_preamble 2.0.2 - package_config 2.1.0 [path] - path 1.9.0 - path_drawing 1.0.1 [vector_math meta path_parsing flutter] - path_parsing 1.0.1 [vector_math meta] - path_provider 2.1.4 [flutter path_provider_android path_provider_foundation path_provider_linux path_provider_platform_interface path_provider_windows] - path_provider_android 2.2.10 [flutter path_provider_platform_interface] - path_provider_foundation 2.4.0 [flutter path_provider_platform_interface] - path_provider_linux 2.2.1 [ffi flutter path path_provider_platform_interface xdg_directories] - path_provider_platform_interface 2.1.2 [flutter platform plugin_platform_interface] - path_provider_windows 2.3.0 [ffi flutter path path_provider_platform_interface] - petitparser 6.0.2 [meta] - platform 3.1.5 - plugin_platform_interface 2.1.8 [meta] - pool 1.5.1 [async stack_trace] - pub_semver 2.1.4 [collection meta] - pubspec_parse 1.3.0 [checked_yaml collection json_annotation pub_semver yaml] - recase 4.1.0 - sanitize_html 2.1.0 [html meta] - shared_preferences_android 2.3.2 [flutter shared_preferences_platform_interface] - shared_preferences_foundation 2.5.2 [flutter shared_preferences_platform_interface] - shared_preferences_linux 2.4.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface] - shared_preferences_platform_interface 2.4.1 [flutter plugin_platform_interface] - shared_preferences_web 2.4.2 [flutter flutter_web_plugins shared_preferences_platform_interface web] - shared_preferences_windows 2.4.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - shelf 1.4.1 [async collection http_parser path stack_trace stream_channel] - shelf_packages_handler 3.0.2 [path shelf shelf_static] - shelf_static 1.1.2 [convert http_parser mime path shelf] - shelf_web_socket 2.0.0 [shelf stream_channel web_socket_channel] - sky_engine 0.0.99 - source_gen 1.5.0 [analyzer async build dart_style glob path source_span yaml] - source_map_stack_trace 2.1.2 [path source_maps stack_trace] - source_maps 0.10.12 [source_span] - source_span 1.10.0 [collection path term_glyph] - sprintf 7.0.0 - stack_trace 1.11.1 [path] - stream_channel 2.1.2 [async] - stream_transform 2.1.0 - string_scanner 1.2.0 [source_span] - term_glyph 1.2.1 - test_api 0.7.2 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph] - test_core 0.6.4 [analyzer args async boolean_selector collection coverage frontend_server_client glob io meta package_config path pool source_map_stack_trace source_maps source_span stack_trace stream_channel test_api vm_service yaml] - timing 1.0.1 [json_annotation] - typed_data 1.3.2 [collection] - url_launcher_android 6.3.10 [flutter url_launcher_platform_interface] - url_launcher_ios 6.3.1 [flutter url_launcher_platform_interface] - url_launcher_linux 3.2.0 [flutter url_launcher_platform_interface] - url_launcher_macos 3.2.0 [flutter url_launcher_platform_interface] - url_launcher_platform_interface 2.3.2 [flutter plugin_platform_interface] - url_launcher_web 2.3.3 [flutter flutter_web_plugins url_launcher_platform_interface web] - url_launcher_windows 3.1.2 [flutter url_launcher_platform_interface] - vector_graphics 1.1.11+1 [flutter http vector_graphics_codec] - vector_graphics_codec 1.1.11+1 - vector_graphics_compiler 1.1.11+1 [args meta path_parsing xml vector_graphics_codec path] - vector_math 2.1.4 - vm_service 14.2.5 - watcher 1.1.0 [async path] - web 1.0.0 - web_socket 0.1.6 [web] - web_socket_channel 3.0.1 [async crypto stream_channel web web_socket] - webkit_inspection_protocol 1.2.1 [logging] - win32 5.5.4 [ffi] - xdg_directories 1.0.4 [meta path] - xml 6.5.0 [collection meta petitparser] - yaml 3.1.2 [collection source_span string_scanner] ```

Additional context and comments

It looks like an issue that already happened in the past:

https://github.com/firebase/flutterfire/issues/11755

Since writing this issue, I've reproduced the error using the latest versions of Flutter / Dart / Firebase.

SelaseKay commented 1 month ago

Hi @ClementCardonnel , thanks for the report. I'm unable to reproduce this issue with your provided sample code. Kindly provide a complete sample code so I can investigate further.

ClementCardonnel commented 1 month ago

Hi! Sorry, @SelaseKay I thought it would be more easily reproducible.

I traced the program with breakpoints to identify the point where the future isn't completed, and I've noticed these lines:

  @override
  Future<ByteData?> send(String channel, ByteData? message) {
    final Completer<ByteData?> completer = Completer<ByteData?>();
    // ui.PlatformDispatcher.instance is accessed directly instead of using
    // ServicesBinding.instance.platformDispatcher because this method might be
    // invoked before any binding is initialized. This issue was reported in
    // #27541. It is not ideal to statically access
    // ui.PlatformDispatcher.instance because the PlatformDispatcher may be
    // dependency injected elsewhere with a different instance. However, static
    // access at this location seems to be the least bad option.
    // TODO(ianh): Use ServicesBinding.instance once we have better diagnostics
    // on that getter.
    ui.PlatformDispatcher.instance.sendPlatformMessage(channel, message, (ByteData? reply) {
      try {
        // This is never called
        completer.complete(reply);
      } catch (exception, stack) {
        FlutterError.reportError(FlutterErrorDetails(
          exception: exception,
          stack: stack,
          library: 'services library',
          context: ErrorDescription('during a platform message response callback'),
        ));
      }
    });
    return completer.future;
  }

Basically, the line completer.complete(reply); is never called.

Here's the value of the channel parameter: dev.flutter.pigeon.cloud_firestore_platform_interface.FirebaseFirestoreHostApi.documentReferenceSet

I was not able to read the value of the message parameter.

I'm sorry I'm not able to provide a more easily reproducible example. It would take me a long time for me to set up a new Firebase project, and since you couldn't reproduce the issue yourself, I expect I won't be able to do so as well. I suppose the issue is linked to how things are on my end, but I don't know which part of my configuration is problematic, so I wouldn't know what to include in a demo project to isolate the issue…

ClementCardonnel commented 1 month ago

There's more:

I assumed the object was created in my database because I was able to see it in the interface in my app. However, checking the Firebase Firestore UI, I noticed that nothing I created from my iOS apps was there. It looks like the documents I'm setting are only set in the local database of my device, but aren't properly sent to the cloud. Which could explain the behavior I'm noticing.

I'm going to update this comment as soon as I have more information to share.

SelaseKay commented 1 month ago

Hi @ClementCardonnel , I tried the steps you described above but it seems to work fine on my end.

ClementCardonnel commented 1 month ago

I've finally been able to potentially isolate the issue. There were two things specific to my project:

  1. I had renamed my Xcode Project from Runner to the name of my app. Seeing that this is not something that's actually compatible with every dependency (FlutterFire for example), I decided to delete my iOS folder and recreate a clean iOS project. But merely doing that didn't fix the issue.

  2. On iOS, I tested this Firebase transaction using a user signed in to Firebase with Sign In With Apple. I tried signing out and using Sign in with Google instead and it finally worked. But then logging back in with my Sign in with Apple user worked too. Very confusing.

The only "coherent" explanation I have is that when recreating my project, I had to add back the Sign in with Apple capability to the Runner target in Xcode. Maybe the capability had been messed up on my previous iOS project after the renaming. And now, adding back the capability fixed the bug?

Nevertheless, I did all of that on an iPhone SE, and then I went to check how it worked on an iPad and I noticed the issue again. I had to do the same thing: log out from Sign in with Apple, log in with Sign in with Google, do a transaction, see that it works, log out, sign back with my Sign in with Apple user, do the same transaction and notice that it finally works (with SIWA).

After that, I signed out, deleted the app on the iPad, re-installed it, signed in with Apple, did a transaction, noticed it worked. Problem solved apparently.

It's hard to pinpoint the exact cause, and thus hard to determine if this is an issue with Firebase, Flutterfire, or general Flutter tooling… But there may be something wrong with the Sign in with Apple implementation in regards to how the credentials are stored on the keychain.

It looks like there are two issues here:

  1. Sign in with Apple credentials might have been invalidated at some point, maybe due to the project renaming and the subsequent hypothetical corruption of the Sign in with Apple capability and/or entitlement.
  2. For this specific case, Firebase silently fails during transactions, providing no error or logs immediately apparent to the user, all of this while hanging indefinitely during await calls.

I'd say that the most probable path to reproduce the issue would be to:

  1. Create a new project, set up sign in with Apple, add the capability within Xcode, write a transaction writing on Firestore.
  2. Run this project on one or more test devices, sign them up, perform the transaction see that it works.
  3. Change the iOS name of the project (the issue here is that depending on how you do it, you may or may not encounter the issue because that's not a straightforward process)
  4. Run the app again, notice that the transaction doesn't work anymore (maybe?, not sure about that?)
  5. Remove the capability and add it back in, sign out the user, delete app, run again, sign back in, notice things are working now…

As you can see that's one hell of reproduction case, all of which without any certainty and for a problem that's likely due to a combination of circumstances and usages very few people are going to encounter. Yeah, not sure if it's worth pursuing. But I figured I'd leave my best rationale of the issue to be as helpful as possible.

Thank you @SelaseKay for being quick to try to reproduce the issue. Knowing that the issue wasn't easily reproducible motivated me to look into it further.

SelaseKay commented 1 month ago

Thank you for the detailed steps. Unfortunately, I'm still unable to reproduce the issue. I'll proceed with closing this for now, but please feel free to reach out if you have any further updates or concerns.

ClementCardonnel commented 1 month ago

Hello. I've been able to simplify the issue. What actually happens is the following:

  1. On iOS, sign in with Google or with Apple
  2. Perform any network call with Firebase (getting data, or setting data): notice that it works.
  3. Close the app (no force-quit needed), and wait for 60 minutes.
  4. Open back the app, try to do any network request. On my end, they fail.

The streams to get data don't send anything, and the transaction to set data infinitely await as described previously. However, it looks like I'm authenticated, at least that's what my UI shows me. And I don't understand why I would not be…

The 60 minutes delay seems to match the one described in the docs. I've console logged my user and it's marked as authenticated. Yet all Firebase network calls fail.

Any luck reproducing the issue with the following procedure?

Thank you in advance

ClementCardonnel commented 1 month ago

OK, I've finally diagnosed and fixed the issue. I've opened a separate issue for clarity since the problem is actually very different from what my initial issue suggested. Here's the new issue: https://github.com/firebase/flutterfire/issues/13405