firebase / firebase-functions

Firebase SDK for Cloud Functions
https://firebase.google.com/docs/functions/
MIT License
1.02k stars 201 forks source link

Firestore add method throws "Error: 13 INTERNAL: An internal error occurred." #1581

Closed dat-huynhtuan-tpv-clv closed 2 months ago

dat-huynhtuan-tpv-clv commented 3 months ago

[REQUIRED] Version info

"engines": {
    "node": "20"
  },
  "dependencies": {
    "axios": "^1.6.5",
    "firebase-functions-test": "^3.1.0",
    "cors": "^2.8.5",
    "firebase": "^10.7.2",
    "firebase-admin": "^11.11.1",
    "firebase-functions": "^4.6.0",
    "googleapis": "^131.0.0",
    "nodemailer": "^6.9.8",
    "eslint": "^8.56.0",
    "eslint-plugin-promise": "^6.1.1"
  }

node: 18.16.1

firebase-functions: 4.6.0

firebase-tools: 13.11.2

firebase-admin: 11.11.1

[REQUIRED] Test case

const { onObjectFinalized } = require('firebase-functions/v2/storage');
const admin = require("firebase-admin");
const { Storage } = require('@google-cloud/storage');
const storage = new Storage({
    projectId: projectId,
    keyFilename: keyFileName
});
admin.initializeApp();

exports.storageInsertEvent = onObjectFinalized({
    bucket: BUCKET[0],
    memory: '1GiB',
    timeoutSeconds: 120
}, async event => {
   try {
        ......
        await admin.firestore().collection(myCollection).add(insertFSObject).then(() => {
            console.log(`FIRESTORE - Object ID: ${event.data.name}`, "[INSERT COMPLETED]");
        }).catch((error) => {
            // console.log(`PATH FILE: ${event.data.name}`, "FIREBASE ERROR");
            console.log(`FIRESTORE - FileName: ${event.data.name}`, "[INSERT FAILURE]");
            console.log(error);
        });
    } catch (error) {
        console.log("Code Error", "[CODE FAILURE]");
        console.log(error);
    }
});

I have checked my codes and logs within the Log Explorer of the current cloud function but before or after the error, the error is no where to be seen again. I also test this code at non-prod environment but also found no clue. This is my error message

Error: 13 INTERNAL: An internal error occurred.
    at callErrorFromStatus (/workspace/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/call.js:31:19)
    at Object.onReceiveStatus (/workspace/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/client.js:192:76)
    at Object.onReceiveStatus (/workspace/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:360:141)
    at Object.onReceiveStatus (/workspace/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:323:181)
    at /workspace/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/resolving-call.js:94:78
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)

[REQUIRED] Steps to reproduce

See my test case section

[REQUIRED] Expected behavior

The function should show success insert like all the logs before or after the error.

[REQUIRED] Actual behavior

Only one error is thrown out, I did check in my firesbase datastore, all the data before and after the error were successfully inserted only one record was not inserted

Were you able to successfully deploy your functions?

Yes. The function was deployed 2 weeks ago and it was doing fine until now

exaby73 commented 3 months ago

node: 8

Hello @dat-huynhtuan-tpv-clv. Sorry if I misunderstood, but are you running Node version 8? Also, you may update your version of firebase-functions as well. That may fix your issue

dat-huynhtuan-tpv-clv commented 3 months ago

node: 8

Hello @dat-huynhtuan-tpv-clv. Sorry if I misunderstood, but are you running Node version 8? Also, you may update your version of firebase-functions as well. That may fix your issue

Hi @exaby73. Sorry it was my typo, node 8 is incorrect it should be node 18.16.1, let me edit the post and I will try to update firebase functions if the error happens again since it is hard to know why the error is popped up.

google-oss-bot commented 2 months ago

Hey @dat-huynhtuan-tpv-clv. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

dat-huynhtuan-tpv-clv commented 2 months ago

Hi @exaby73, updated for you on this post. I think I know what is wrong to my cloud functions, it seems like my cloud function isn't idempotent and the google cloud storage sometimes trigger the same function again on the same object so firebase is handling the same response twice and it could make the error to pop up. After making the cloud function idempotent I also move the admin.initializeApp(); inside the cloud function because based on what I've observed the cloud functions can be triggered multiple times at the same object so I think it is not the best option to leave it as globally.

exaby73 commented 2 months ago

You can also use the new onInit function to run code on each startup. It's a good place to have initializeApp. Since you were able to solve your issue, I will close this issue. Thank you