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

Firestore emulator Requests Monitor shows many almost identical requests.

Open yukihiroK opened this issue 3 years ago • 9 comments

When you fetch data using the firebase/firestore library in a browser, the request will appear in the monitor. However, when subscribing to changes via the onSnapshot method, somehow multiple identical requests appear in the monitor. Since these request.times are slightly different, it may be that the rule evaluation has been performed multiple times. This number of times increases by one each time you reload the page. It then returns to zero by restarting the emulator.

Note that the first read of onSnapshot is not a problem. The subsequent request when subscribing to the changes is the problem.

[REQUIRED] Environment info

firebase-tools: 10.3.1

Platform: macOS Big Sur 11.6.4

[REQUIRED] Test case

I made a sample project to reproduce this problem. https://github.com/yukihiroK/firestore-sample

useEffect(() => {
		const unsubscribe = onSnapshot(docRef, (doc) => {
			if (doc.exists()) {
				const data = doc.data();
				console.log(data);
				setState(data);
			}
		});
		return unsubscribe;
	}, []);

Listen to a specific document or collection with the onSnapshot method and make changes to that document on the Firestore.

[REQUIRED] Steps to reproduce

Use the sample program above.

  1. Start firebase emulator suite.
  2. Create a document in /(root)/collection/document/sub/doc1 on the firestore.
  3. Edit the firestore.rules file to allow reading and writing to the document.
  4. Start the sample react program and access http://localhost:3000/.
  5. Press the "Add KV" button that appears on the screen, change the document, and subscribe to the change with the onSnapshot method.
  6. Check the Requests Monitor. http://localhost:4000/firestore/requests

[REQUIRED] Expected behavior

I think the rule evaluation should be done only once, like when the page has never been reloaded.

[REQUIRED] Actual behavior

screenshot

yukihiroK avatar Mar 18 '22 08:03 yukihiroK

Hi, thanks for filing this issue! Technically speaking, the Firestore Emulator does evaluate rules per listener since they may lead to different results (depending on query / auth / etc.), but I can totally see how this can be noisy.

I'm filing an internal bug for some better way to group / clean this up. We are unable to promise any timeline for this, but if others also have this request, adding a +1 on this issue can help us prioritize adding this to the roadmap.

(Googler-only internal tracking bug: b/226158969)

yuchenshi avatar Mar 22 '22 18:03 yuchenshi

I just encountered the exact same problem. Any news on a fix? Do the problem only affect the emulator?

prodisoftdev avatar Jul 13 '22 19:07 prodisoftdev

I just encountered the exact same problem. Any news on a fix? Do the problem only affect the emulator?

This is an emulator-only issue. Just to clarify, this emulator-only view in the Emulator UI shows one entry per rules evaluation, which happens for each listener per request.

yuchenshi avatar Jul 18 '22 15:07 yuchenshi

+1

I just ran into this as well. Before seeing this issue, I assumed there was a problem with my code that would ultimately produce tons of extra requests and, therefore, a significantly higher GCP bill (I'm still a little bit paranoid about it).

In the meantime, it would be much appreciated if you could explain in a little bit more detail why this is happening... Thank you!

zachcaldwell avatar Oct 11 '22 15:10 zachcaldwell

+1 I'm also experiencing this issue with Firestore snapshots in the emulator. Resets when the emulator is restarted. Also gave me a scare re the GCP bill!

bryce-marshall avatar Oct 10 '23 01:10 bryce-marshall

+1

DavidWeiss2 avatar Oct 22 '23 16:10 DavidWeiss2

+1 This problem is interfering with the prediction of the number of requests, and I think it is a problem that scares the programmers and makes them lose potential paying users in the future.

TsubasaTommy avatar Nov 26 '23 15:11 TsubasaTommy

1+. I have the same issue. Even tho in the Network tab on the browser only shows one request, it keeps incrementing when I delete users and add new users (where these users has its own collectioon on firestore)

luispellizzon avatar Feb 02 '24 20:02 luispellizzon