firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

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

Open SDrinkwater opened this issue 3 years ago • 11 comments

[REQUIRED] Describe your environment

  • Operating System version: macOS Catalina 10.15.6
  • Browser version: Chrome 85.0.4183.102
  • Firebase SDK version: 7.20.0
  • Firebase Product: Firestore

[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 avatar Sep 24 '20 11:09 SDrinkwater

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.

SDrinkwater avatar Sep 27 '20 01:09 SDrinkwater

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 avatar Oct 01 '20 04:10 var-const

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!

var-const avatar Oct 05 '20 19:10 var-const

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?

yuchenshi avatar Oct 20 '20 18:10 yuchenshi

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!

google-oss-bot avatar Oct 27 '20 01:10 google-oss-bot

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

lexylamont avatar Oct 29 '20 02:10 lexylamont

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!

google-oss-bot avatar Nov 05 '20 02:11 google-oss-bot

Any updates? This is really annoying.

EthanSK avatar May 17 '21 19:05 EthanSK

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.

amkoehler avatar Jun 18 '21 19:06 amkoehler

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

jpangburn avatar Mar 17 '22 05:03 jpangburn

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.

vinz avatar Aug 23 '22 06:08 vinz