firebase / firebase/firebase-tools
Firestore emulator calls functions twice on startup
- Dominant language
- TypeScript
- Stars
- 4.5k
- Forks
- 1.3k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 84
Description
### [REQUIRED] Environment info
**firebase-tools:** 14.6.0
**Platform:** Ubuntu 24.10
### [REQUIRED] Test case
Create a python functions instance with `initialize_app()` and `db = firestore.client()`. Create a function that inserts 100 documents in a while loop (also works with batched write) inserting with `db.collection("collection_name").document().set()` start emulator with `firebase emulators:start`
### [REQUIRED] Steps to reproduce
Run endpoint with curl or API testing tool (I'm using insomnia).
### [REQUIRED] Expected behavior
Expect inserts to be called once
### [REQUIRED] Actual behavior
Function is run twice, inserting documents twice. When the function runs twice, I see `Jun 18, 2025 9:03:27 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.` in firestore-debug.log. I see this when running other functions for the first time, including read all with fdb.collection("collection_name").stream() and always get the channelRead log when runs twice, but after running it again it runs once as expected with no log.
I've seen these articles https://firebase.google.com/docs/functions/tips https://cloud.google.com/blog/products/serverless/cloud-functions-pro-tips-building-idempotent-functions suggesting idempotency but they imply this is only an issue for triggered background functions which have an eventID, these calls do not have an eventID. This video ( mostly the one before it https://www.youtube.com/watch?v=MzTS6mFDGjU&list=PLl-K7zZEsYLm9A9rcHb1IkyQUu6QwbjdM&t=3m22s just briefly says "background functions") also switches from cloud Functions to triggers/background functions when mentioning there could be duplicate runs https://www.youtube.com/watch?v=Pwsy8XR7HNE&t=12s And anything else I could find blamed CORS which would happen every request not once and stop, and the only http status I see is 200 not 204 like they say for CORS.
So I don't think this should be the behavior
Contributor guide
Research direction
Start with the minimal Python functions reproducer using initialize_app(), firestore.client(), and firebase emulators:start, then invoke the endpoint with curl and compare the first and subsequent runs. Inspect firestore-debug.log around the HTTP/2 channelRead message; done means each HTTP invocation performs the inserts once rather than twice.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, typescript
- Domain
- cli, databases, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100