Skip to content

🐛 [cloud_firestore] Retrieving a document that doesn't exist while offline permanently throws an exception (and breaks apps) #10887

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

Closed
stx opened this issue May 2, 2023 · 12 comments
Assignees
Labels
closed-by-bot plugin: cloud_firestore resolution: needs-repro This issue could not be reproduced or needs an up to date reproduction on latest FlutterFire plugin. resolution: no-response Customer did not respond after some time. Stale Issue with no recent activity type: bug Something isn't working

Comments

@stx
Copy link

stx commented May 2, 2023

Bug report

When attempting to get a document that doesn't exist while offline, the error below will be permanently thrown even though the cache is populated correctly, and even after returning online:

[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.

This issue has been ongoing for years in a discussion @ #5708 but has not been fully identified or understood until now. Thanks to @cammoore54 here we now have reproduction steps.

We can also confirm that this is a critical app breaking issue. In our commercial app, with over a thousand instances recorded across many cloud_firestore and Flutter versions, and it prevents users from loading data.

This causes apps to break since if a user tries to log in or do anything with a new document while offline, they will receive cloud_firestore/unavailable, but will still receive it when going back online until the app is killed or uninstalled and reinstalled.

Steps to reproduce

Credit to @cammoore54:

The below DOESN'T raise the error:

var collectionRef = FirebaseFirestore.instance.collection('tags');
var tags = await collectionRef.get(GetOptions(source: source)));

The below DOES raise the error (only when offline):

var collectionRef = FirebaseFirestore.instance.collection('tags');
var doc = await collectionRef.doc(id).get(GetOptions(source: source)); //where id doesn't exist for this collection

For both of the above I have confirmed that the correct data is stored in the cache when offline.

Making this call online first, then going offline doesn't cause the error but if the device is offline the first time the call is made raises the error.

Expected behavior

No error should be thrown since the cache is populated and working in offline mode.

@stx stx added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels May 2, 2023
@darshankawar darshankawar added the triage Issue is currently being triaged. label May 3, 2023
@darshankawar
Copy link

Thanks for the report. Using the code sample provided and trying to run in offline mode gave me the same reported error:

[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.

More reference on the issue: #5708

@darshankawar
Copy link

/cc @Lyokone

@darshankawar darshankawar added plugin: cloud_firestore and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. labels May 3, 2023
@gsiewe
Copy link

gsiewe commented Jun 14, 2023

Any update on this issue? Thousands of my users are facing this issue.

@Chuckame
Copy link

No need to go offline, just do a get(GetOptions(source: Source.cache)) and then the same error comes.

@Mijawel
Copy link

Mijawel commented Aug 9, 2023

Facing a similar error but slightly different message.

[cloud_firestore/unavailable] Failed to get documents from server. (However, these documents mi exist in the local cache. Run again without setting source to "server" to retrieve the cached documents.)

@russellwheatley
Copy link
Member

Could you specify what platform(s) this is occurring on?

@russellwheatley russellwheatley self-assigned this Sep 21, 2023
@Mijawel
Copy link

Mijawel commented Sep 21, 2023

Web

@russellwheatley
Copy link
Member

I'm going to need a reproduction, I've just tested the following Flutter app in Firestore example main.dart

// Copyright 2020, the Chromium project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'firebase_options.dart';

/// Requires that a Firestore emulator is running locally.
/// See https://firebase.flutter.dev/docs/firestore/usage#emulator-usage
bool shouldUseFirestoreEmulator = false;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  if (kIsWeb) {
    await FirebaseFirestore.instance
        .enablePersistence(const PersistenceSettings(synchronizeTabs: true));
  }
  if (shouldUseFirestoreEmulator) {
    FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
  }
  runApp(FirestoreExampleApp());
}

/// The entry point of the application.
///
/// Returns a [MaterialApp].
class FirestoreExampleApp extends StatelessWidget {
  final collection = FirebaseFirestore.instance.collection('flutter-tests');
  final doc = 'new-one';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Firestore Example App',
      theme: ThemeData.dark(),
      home: Scaffold(
          body: Column(
        children: [
          const SizedBox(height: 50),
          ElevatedButton(
            onPressed: () async {
              // 1. set device to offline, add document to cached collection
              await collection.doc(doc).set({'name': 'new-one'});

              print('Set in cache');
            },
            child: const Text('set cached doc'),
          ),
          ElevatedButton(
            onPressed: () async {
              // 2. get specific document with ID from cache
              final result = await collection
                  .doc(doc)
                  .get(const GetOptions(source: Source.cache));
              print('LLLLL: ${result.data()}');
            },
            child: const Text('get cached document'),
          ),
        ],
      )),
    );
  }
}

Steps:

  1. Run Firestore example app
  2. Switch internet off
  3. Add document ("set cached doc" button)
  4. Retrieve document

Across android, ios and web, they all received the cached document.

@russellwheatley russellwheatley added the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Sep 22, 2023
@stx
Copy link
Author

stx commented Sep 22, 2023

@russellwheatley Did you try without specifying Source? In our app and in other reports that isn't being specified and it's using the defaults.

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Sep 22, 2023
@russellwheatley
Copy link
Member

russellwheatley commented Sep 22, 2023

I didn't because in your initial post you specified with source as a way to reproduce. Please provide an exact code sample for me to drop into the Firestore example and let me know what platforms this is occurring on for you.

@russellwheatley russellwheatley added the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Oct 13, 2023
@google-oss-bot google-oss-bot added the Stale Issue with no recent activity label Oct 16, 2023
@google-oss-bot
Copy link

Hey @stx. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

@google-oss-bot
Copy link

Since there haven't been any recent updates here, I am going to close this issue.

@stx if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

@darshankawar darshankawar added resolution: needs-repro This issue could not be reproduced or needs an up to date reproduction on latest FlutterFire plugin. resolution: no-response Customer did not respond after some time. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. Needs Attention This issue needs maintainer attention. labels Oct 25, 2023
@firebase firebase locked and limited conversation to collaborators Nov 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
closed-by-bot plugin: cloud_firestore resolution: needs-repro This issue could not be reproduced or needs an up to date reproduction on latest FlutterFire plugin. resolution: no-response Customer did not respond after some time. Stale Issue with no recent activity type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants