firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

[Firestore Emulator] onSnapshot triggered with a nonexistent doc on doc creation

Open tavoyne opened this issue 4 years ago • 4 comments

[REQUIRED] Environment info

firebase: 9.1.2

firebase-tools: 9.21.0

Platform: macOS Big Sur (11.6)

[REQUIRED] Test case

I'm experiencing a bug using the Firestore Emulator. On document creation, the onSnapshot listener (that was set up beforehand) is triggered twice:

  • The first time with the expected document (e.g. the one that was just created). It has the hasPendingWrites metadata property set to true, which is expected.
  • The second time with an empty document (snapshot.exists() is false), whereas the document was successfully created and is visible in the Emulator UI. The hasPendingWrites metadata is false in this case, which is expected. If I refresh the page however, the document is correctly fetched.

This never happened to me in production so I think it's an emulator-specific issue. Also, it seems to happen only once, when the emulator was just started. In other words, subsequent document creations seem not to be affected by this bug.

[REQUIRED] Steps to reproduce

N/A

[REQUIRED] Expected behavior

I'm expecting the snapshot attached the second onSnapshot triggering (the hasPendingWrites: false one) to exist and its data to match the created document.

[REQUIRED] Actual behavior

See above.

tavoyne avatar Oct 26 '21 13:10 tavoyne

Can you please provide a repro script / repo?

abeisgoat avatar Nov 08 '21 21:11 abeisgoat

Of course. I'll put up a demo later today or tomorrow.

tavoyne avatar Nov 09 '21 10:11 tavoyne

Facing the same issue here

ggirotto avatar Nov 09 '21 12:11 ggirotto

I wanted to create a minimal reproduction but the problem has been absolutely impossible to narrow down. I just spent 72 hours on this. There seems to be a kind of race condition that involves the Emulator start up that causes the onSnapshot listener to be triggered with inaccurate data. It's just so unpredictable that I can't properly identify the origin. I'd love to be in touch with someone in the team and discuss the matter live with my code (which is legit). It's pretty simple, and as you guys know how it works under the hood, you might be able to see what edge case I've reached.

tavoyne avatar Nov 14 '21 18:11 tavoyne