3

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.

3
  • This code should not timeout as it's very simple, is the function only deployed in one region? Is it the closest possible to you? You could try deploying it to a second region as a test to check if this keeps timing out. Commented Jan 15, 2021 at 13:48
  • I'm getting the exact error. Not related to deployment - this is on the Firebase Emulator. Other functions work well. Commented Jan 15, 2021 at 14:11
  • @RafaelLemos I am using the firebase emulator. so I do not think it is an issue of latency. Commented Jan 16, 2021 at 0:56

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I am simply running the node script which writes a minimum of 1000 records to the firestore. It is perfectly running on the local system! But it is throwing a timeout error while testing on the prod. Any idea regarding this?
I'm pretty sure I'm experiencing the same issue in Dec 2022
0

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

1 Comment

This actually... worked... I had functions timing out in the emulator regardless of what they actually did. This solved it for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.