Skip to content

About

Demonstrates bug where snapshotsInSync() is fired too early when using the Firestore Emulator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

snapshotsInSync() fires mid-batch after calling enableNetwork() when using the Firestore emulator

The snapshots-in-sync event indicates that all listeners affected by a given change have fired, even if a single server-generated change affects multiple listeners.

When a client calls disableNetwork() and then later enableNetwork() snapshotsInSync() seems to fire before all listeners affected by a single change have been notified. This only happens when testing against the Firestore emulator. When testing against a real backend snapshotsInSync() behaves as expected.

Run it

Requires Node.js and the Firebase CLI (npm install -g firebase-tools), which runs the emulator. The emulator also needs Java.

npm install

Two terminals:

# 1. the emulator
firebase emulators:start --only firestore --project demo-emulator-bug

# 2. the repro
node repro.mjs

It runs CYCLES (default 20) cycles, prints how many sync events fired while the two collections disagreed, and exits.

What each cycle does

The script creates two independent clients: a writer and a listener. The listener watches the collection_a and collection_b collections and registers a snapshotsInSync() listener.

  1. MODE=reconnect (default): the listener calls disableNetwork().
  2. The writer commits one writeBatch creating collection_a/<run>-<cycle> and collection_b/<run>-<cycle>, and waits for the commit to complete.
  3. MODE=reconnect: the listener calls enableNetwork(), so the whole batch reaches it as one catch-up. MODE=steady skips steps 1 and 3, so the batch arrives at a continuously connected client.
  4. The listener waits until both collections have received the batch.

If snapshotsInSync() fires before both listeners have received the same update, the output is marked <-- INCONSISTENT to indicate that snapshotsInSync() has fired too early.

Reading the output

The shape to look for (illustrative, not a recorded run):

[09:01:04.411] cycle 7: committed one batch to collection_a + collection_b
[09:01:04.423]   snapshot collection_a: cycle 7 arrived
[09:01:04.423]   snapshotsInSync() fired  [collection_a=7, collection_b=6]   <-- INCONSISTENT
[09:01:04.424]   snapshot collection_b: cycle 7 arrived
[09:01:04.425]   snapshotsInSync() fired  [collection_a=7, collection_b=7]

=== mode=reconnect target=emulator cycles=20 ===
sync events while the collections disagreed: 20
firebase-js-sdk 13.x.x on node v23.11.0

When reporting results, also include the output of firebase --version.

Options (environment variables)

variable default meaning
TARGET emulator emulator (at localhost:8080) or real
MODE reconnect reconnect or steady (see above)
CYCLES 20 how many batches to commit

Against a real Firebase project

The same program, pointed at a real backend, is the control group and the bug is not observed:

TARGET=real FIREBASE_PROJECT_ID=... FIREBASE_API_KEY=... node repro.mjs

The project's security rules have to permit collection_a and collection_b.

About

Demonstrates bug where snapshotsInSync() is fired too early when using the Firestore Emulator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages