Repository navigation
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(<doc>) 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
Activity
aalej commentedon Jun 19, 2025
Hey @ryanadrig, thanks for reporting this and for sharing your observations. Let me know in case I misunderstood, but just to verify, are you invoking an https functions that creates 100 docs in Firestore emulator? Something like:
And based on your observations, this function runs twice on its initial invocation(so it creates 200 docs instead of 100), but only once on subsequent runs.
Also, which version of
pythonandfirebase_functionsare you using?ryanadrig commentedon Jun 19, 2025
@aalej Hi thanks for looking at this! Yes that is exactly it, was maybe expecting to get RTMd as expected behavior but couldn't find it in docs, so at minimum maybe a docs update, most basic setup I can get, with the addition of
fdb = firestore.client()I call the loopand it completes in under 1 second with status 200 response, twice in the mentioned fashion...
versions ~~
Python 3.12.7
pip show firebase-adminName: firebase-admin
Version: 6.9.0
pip freezeselected output...
firebase-admin==6.9.0
firebase-functions==0.1.2
google-api-core==2.25.0
google-api-python-client==2.171.0
google-auth==2.40.3
google-auth-httplib2==0.2.0
google-cloud-core==2.4.3
google-cloud-firestore==2.21.0
google-cloud-storage==3.1.0
google-crc32c==1.7.1
google-events==0.14.0
google-resumable-media==2.7.2
googleapis-common-protos==1.70.0
...
grpcio==1.72.1
grpcio-status==1.72.1
gunicorn==23.0.0
h11==0.16.0
h2==4.2.0
hpack==4.1.0
httpcore==1.0.9
httplib2==0.22.0
httpx==0.28.1
...
aalej commentedon Jun 19, 2025
Thanks for verifying @ryanadrig! I'm able to reproduce the reported behavior, it looks like this is an issue with the Functions Emulator. As you mentioned, when invoking the function for the first time, it always runs twice. Even a simple https trigger function like below seems to run twice
When running
curl http://127.0.0.1:5001/demo-project/us-central1/py_testfor the first time,PRINT_STATEMENT - SHOULD ONLY APPEAR ONCE!!!is printed twiceI'm also not able to reproduce the issue when emulating a JavaScript function, so this seems like a bug specific to emulating Python Functions.
I'll raise this to our engineering team so they can take a look. Sharing the mcve here
Emanuel-Bissacotti commentedon Aug 26, 2025
I have the same problem, any solution for this?
ryanadrig commentedon Aug 27, 2025
@Emanuel-Bissacotti In the mean time I used this, has been working for me to avoid duplication
andersonaddo commentedon Jun 17, 2026
@aalej this is still a problem
For folks still encountering this:
I have this decorator I call on my on_request functions to prevent this