firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.01k stars 926 forks source link

Firestore emulator Requests Monitor shows many almost identical requests. #4325

Open yukihiroK opened 2 years ago

yukihiroK commented 2 years ago

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
yuchenshi commented 2 years ago

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)

prodisoftdev commented 2 years ago

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

yuchenshi commented 2 years ago

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.

zachcaldwell commented 1 year ago

+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!

bryce-marshall commented 11 months ago

+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!

DavidWeiss2 commented 10 months ago

+1

TsubasaTommy commented 9 months ago

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

luispellizzon commented 7 months ago

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)

Bindslev commented 3 months ago

+1

HammadAwan424 commented 1 month ago

+1