firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.82k stars 884 forks source link

Documents deleted with a function don't update the local cache if persistence is enabled #3838

Open SDrinkwater opened 3 years ago

SDrinkwater commented 3 years ago

[REQUIRED] Describe your environment

[REQUIRED] Describe the problem

Documents deleted with a function don't update the local cache if persistence is enabled.

I am running the firebase emulator. I call a firebase function that deletes an entire collection of documents. The collection disappears from the emulator and when persistence is not enabled, the documents are now gone from query results.

However, when persistence is enabled, Firestore returns those documents when queried, regardless of the specified source. For example, if I specify server as the source, the resulting list of documents still contains those deleted documents. The query metadata indicates that the query results are from the server, but that is not the case. Here is the metadata when source is set to server: { fromCache: false, hasPendingWrites: false }. This is correct, but the collection of returned docs is not.

If I disable persistence, the problem goes away and I am returned the fresh collection - empty as expected.

This is a pretty serious bug that prevents anyone from using Firestore persistence in conjunction with firebase functions that manipulate document collections.

Steps to reproduce:

  1. Create a firebase function that deletes a collection of documents.
  2. Enable persistence on the client.
  3. Fetch collection of documents.
  4. Call function that deletes the same collection of documents.
  5. Fetch collection of documents on client with source set to server.
  6. Notice that the query metadata indicates that query results are from the server { fromCache: false, hasPendingWrites: false } but the collection of documents includes all of the documents deleted by the function.

I can't seem to get the latest results from that collection (I expect to receive an empty result) unless I disable persistence, at which point the query result returns an empty list, as expected.

SDrinkwater commented 3 years ago

The issue seems to be directly related to the emulator. Once the function is deployed and I read the docs from the remote firestore, everything seems to work as expected.

var-const commented 3 years ago

Sorry to hear about your trouble. I was able to reproduce this with an emulator using the reproduction steps you outlined, will post back once I have more information.

var-const commented 3 years ago

Update: this appears to be a bug in the emulator. It may be triggered by deleting documents via the emulator UI as well. I will reroute to somebody working on the emulator (the internal bug is b/170145142). Thank you for reporting the issue!

yuchenshi commented 3 years ago

As per the documentation, local persistence should be turned off when working with the Firestore Emulator, exactly for reasons you've discovered.

Note: The Cloud Firestore emulator clears database contents when shut down. Since the offline cache of the Firestore SDK is not automatically cleared, you may want to disable local persistence in your emulator configuration to avoid discrepancies between the emulated database and local caches; in the Web SDK, persistence is disabled by default.

I can definitely look into the issue when I have more bandwidth, but is there any reason that you want persistence to be enabled?

google-oss-bot commented 3 years ago

Hey @SDrinkwater. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 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!

lexylamont commented 3 years ago

Hi, please don't close this. The reason this needs to be fixed, is so that local tests have integrity.

google-oss-bot commented 3 years ago

Hey @SDrinkwater. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 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!

EthanSK commented 3 years ago

Any updates? This is really annoying.

amkoehler commented 3 years ago

Is this confirmed to only be an issue in the emulator? We have a cleanup script that cleans up certain documents as a nightly cron job, and this sometimes breaks the users' persisted cache. The only solution is to delete the IndexedDB database for firestore from dev tools.

jpangburn commented 2 years ago

Hi, please don't close this. The reason this needs to be fixed, is so that local tests have integrity.

This is exactly the issue. When we build unit tests and integration tests, you want confidence that it'll behave the same way on real Firestore as it does on the emulator. Disabling persistence does make this problem go away (I see it on iOS where persistence is enabled by default), but it gives you less confidence in your tests because your test client(s) is configured to behave differently than a real client.

I think this problem is not with the firebase-js-sdk though because I see it in FlutterFire in iOS which delegates to the iOS Firebase SDK. Unless it's just a bug that's been copied to all the client platforms...

vinz commented 2 years ago

Can't believe this issue is unresolved even after 2 years! I am still facing this issue. Team, please take a look and fix this.