Firebase function timeout regardless of code

Viewed 1713

Edit: 2021 feb this problem was fixed.

I have been dealing with a timeout error using node js firebase function emulator. My function was working, but now the code will wait for a timeout regardless of the code. I tried copying the example on the quick start page, and the same error is occurring.

I can put console statements in the code, and I will see nothing output. I have another function that works properly when a document is created. The response errors out, but the function will continue executing for the duration of the timeout.

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();
exports.addMessage = functions.https.onRequest(async (req, res) => {
    // Grab the text parameter.
    const original = req.query.text;
    // Push the new message into Firestore using the Firebase Admin SDK.
    const writeResult = await admin.firestore().collection('messages').add({original: original});
    // Send back a message that we've successfully written the message
    res.json({result: `Message with ID: ${writeResult.id} added.`});
  });

Error: Function timed out. at Timeout._onTimeout (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:640:19) at listOnTimeout (internal/timers.js:554:17) at processTimers (internal/timers.js:497:7) i functions: Beginning execution of "createTokenForEvents" ⚠ functions: Your function timed out after ~60s. To configure this timeout, see https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation. /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:640 throw new Error("Function timed out."); ^

Edit: I have come to the conclusion that this function does work, but it just always error out. I was writing another function when I discover this error and even when I switch to this simple case, the error was present. However, in this case, the function does create a document, but whatever is keeping it on for the entire duration may also be hiding log statements too. My question has change; why is the function executing for the entire duration even after the code completes.

2 Answers

https://firebase.google.com/docs/functions/terminate-functions

As stated in the documentation, one of the principles of writing a good functions is

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

Since my other functions where synchronous, writing a return statement was good enough. However, I solve the error by explicitly adding terminating response statement res.end(). I also experiment with returning a promise, but it did not solve the problem either.

Edit: This problem continues to occur in different scenarios. I literally cannot get the code to run when using a query https://github.com/firebase/firebase-functions/issues/847. As of Jan 2021, the firebase-functions repo seems to be unmaintained as far as fixing community issues. A user got a response from the firebase team saying they have been trying to fix the logging issues, but they would not commit to a date on the problem would be resolved. I would just not even bother using the firebase functions emulator. Deploying the function works fine.

Due to the query parameter, add the slash symbol:

  • http://localhost:5001/.../us-central1/helloWorld/?foo=bar&test=string
  • http://localhost:5001/.../us-central1/helloWorld?foo=bar&test=string (no slash before ?)

Refer from https://github.com/firebase/firebase-tools/issues/1314

Related