Firebase-functions: Every function is run twice

Created on 23 Nov 2019  Â·  8Comments  Â·  Source: firebase/firebase-functions

Related issues

[REQUIRED] Version info

node: 8.16.2 (package.json node engine set to 8)

firebase-functions: 3.3.0

firebase-tools: 7.8.1

firebase-admin: 8.6.1

Firebase (Web Client): 7.4.0

[REQUIRED] Test case

Run a callable function (e.g. using var myServerFunction= mainFirebase.functions().httpsCallable('myServerFunction');)

[REQUIRED] Steps to reproduce

Run a callable function.

[REQUIRED] Expected behavior

Function gets called and executed once.

[REQUIRED] Actual behavior

The function is run twice, with the first time instantly returning a 204:

Function execution started
Function execution took 23 ms, finished with status code: 204

then right after:

Function execution started
Functon execution took 1432 ms, finished with status code: 200

The second execution runs fine. My logs are filled with tons of the first execution of every function just running and returning a 204.

Were you able to successfully deploy your functions?

There are no error messages. Deploys work perfectly.

Author Feedback

Most helpful comment

If you are calling the function from the browser, this is expected behavior because the browser is going to be making a CORS preflight OPTIONS request to ensure that the function is willing to serve cross-origin content. This request is automatically handled for you by the wrapping logic of the callable function, so your code doesn't get executed but that is in fact a legitimate and necessary request for a browser to be able to call the function.

All 8 comments

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@theryan722 I apologize for the slow response here. I have a few clarifying questions:

  • Is this issue happening to you in production? Does it happen if you deploy a very simple "hello world" callable function or only with your own code? I am surprised to see a 204 result.
  • Can you show a little more of the JavaScript code where you're calling the callable function?

If you are calling the function from the browser, this is expected behavior because the browser is going to be making a CORS preflight OPTIONS request to ensure that the function is willing to serve cross-origin content. This request is automatically handled for you by the wrapping logic of the callable function, so your code doesn't get executed but that is in fact a legitimate and necessary request for a browser to be able to call the function.

Thanks for the clarification, that makes sense. I appreciate the answer, I learned something new as well!

@mbleigh : Will this double our Firebase function invocation cost? Another scenario could be that when we have one project multiple sites and we prefer to store our cloud functions for different site in different .JS file. To do that, we export all the function signatures in index.js -- Would that cause a double invocation as well and will it double the invocation cost? Thanks.

CORS requires two requests whenever a browser is making a "cross-origin" request. The origin is everything before the path in a URL, so https://example.com and https://subdomain.example.com are two different origins, but https://example.com/foo and https://example.com/bar are not.

The browser makes an OPTIONS requests always for cross-origin network calls except in "basic" cases where there are no custom headers etc. Since callable functions always include an Authorization header, they will always trigger a CORS OPTIONS request.

So yes, CORS requests will cause two "invocations" of a Cloud Function for billing purposes.

Since callable functions always include an Authorization header, they will always trigger a CORS OPTIONS request.

You mean for Https Callable Functions, even though same origin request, it will trigger CORS OPTIONS and lead to double invocation costs too? I'm currently running localhost with the latest version of Firebase Emulators, making same origin requests, and it causes double invocations too.

On localhost different ports are considered different origins.

On Tue, Sep 15, 2020, 11:15 PM Antonio Ooi notifications@github.com wrote:

Since callable functions always include an Authorization header, they
will always trigger a CORS OPTIONS request.

You mean for Https Callable Functions, even though same origin request, it
will trigger CORS OPTIONS and lead to double invocation costs too? I'm
currently running localhost with the latest version of Firebase Emulators,
making same origin requests, and it causes double invocations too.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/firebase/firebase-functions/issues/584#issuecomment-693197224,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAAAH7RHNLA7IJORLDSFVZ3SGBJYLANCNFSM4JQYG6GQ
.

Was this page helpful?
0 / 5 - 0 ratings