Firebase cloud function exits with code 16 what is error code 16 and where can I find more info?

15,991

Solution 1

The code for the function framework is actually public in the GoogleCloudPlatform/functions-framework-nodejs repository (although not advertised anywhere).

In particular you can see there the cases where killInstance is used, which is the one triggering exit code 16:

const killInstance = process.exit.bind(process, 16);

These cases are (at the time of writing):

Solution 2

Apparently and after investigation, the error means something like: Headers already sent.

I had somewhere in my code a response.send() with headers and then, later on, I was sending another response.

Would be great if the Firebase team could elaborate with these issues and provide some documentation instead of leaving us blindfolded (to my understanding)

Solution 3

An improperly terminated cloud function is the likely cause.

Here's what the firebase docs say:

  • Resolve functions that perform asynchronous processing (also known as "background functions") by returning a JavaScript promise.

  • Terminate HTTP functions with res.redirect(), res.send(), or res.end().

  • Terminate a synchronous function with a return; statement.

In short, watch out for floating promises and/or multiple calls to res or response.

I've also seen the Process exited with code 16 occur in tandem with:

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync (/srv/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Solution 4

Had the same issue. As @cDitch mentions my code was not 100% complete. However the biggest issue for me were outdated packages.

I needed to upgrade firebase-admin, firebase-functions and firebase-tools as well as eslint.

you can see which packages are outdated by running:

npm outdated

Then i've changed the dependencies manually inside the package.json to the latest version mentioned by npm outdated.

It's possible that it'll cause deploy issues after you do this. At least this is what happened to me. Completely removing the node_modules and reinstalling them fixed this.

Here's two lines i've added to my package.json scripts to do this on windows:

"clean": "rmdir /s /q node_modules",
"reinstall": "npm run clean && npm install",
  • rmdir -> delete a directory
  • /s -> delete the whole tree (so all folder within)
  • /q -> do this quietly so you dont flood your terminal and have to wait all printed lines.

now you can run the following command

npm run clean
npm install

or

npm run reinstall

to execute those steps.

Share:
15,991
Jimmy Kane
Author by

Jimmy Kane

GitHub profile Google+ profile

Updated on June 03, 2022

Comments

  • Jimmy Kane
    Jimmy Kane about 2 years

    one of my cloud functions on firebase exits with code 16 as an error.

    I tried to google and find out what that code is but no luck at all.

    Error: Process exited with code 16
        at process.on.code (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:393:29)
        at process.emit (events.js:189:13)
        at process.EventEmitter.emit (domain.js:441:20)
        at process.exit (internal/process/per_thread.js:168:15)
        at logAndSendError (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:184:9)
        at process.on.err (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:390:13)
        at process.emit (events.js:189:13)
        at process.EventEmitter.emit (domain.js:441:20)
        at emitPromiseRejectionWarnings (internal/process/promises.js:119:20)
        at process._tickCallback (internal/process/next_tick.js:69:34)
    
    

    Where can I find those error codes reported so I can understand why my function exits?

  • Leo
    Leo over 4 years
    I got hit by this today (again, forgot a return after an error). Where did you find the error codes? Did you report it to the Firebase team?
  • Jimmy Kane
    Jimmy Kane over 4 years
    @Leo check the cloud console (google) that posts the codes there
  • Leo
    Leo over 4 years
    Thanks, but I mean the explanation of for example code 16. I only saw something like what you posted on the question.
  • Jimmy Kane
    Jimmy Kane over 3 years
    Are you sure these are the 2 options only? And not having already sent the headers? If yes I need to mark this as correct and not my answer.
  • Gluck
    Gluck over 3 years
    yes, this is the only place where exit(16) is called. That being said it could be possible that duplicate headers trigger an exception, which could appear in an un-catched async block of your function, hence triggering unhandledRejection, hence triggering this behavior, but the first hint/answer shouldn't be this one (it wasn't in my case)
  • Jimmy Kane
    Jimmy Kane over 3 years
    Let me mark yours as valid, because its more complete , and holds this info. Thank. you
  • Mehmet Efe Akça
    Mehmet Efe Akça about 3 years
    This was the correct answer for me. I encountered this error when I forgot to write await in a try-catch block so there was an uncaught rejection.