2

I have a google cloud function that's seems to timeout after being inactive for a certain amount of time or if I re-deploy it. Subsequent calls to the end point work just fine, it's just the initial invocation which doesn't work. The following is an over simplified version of what my cloud function is. I basically use an express app as a handler. Perhaps the issue is with the express app not running the first time around, but running on subsequent invocations?

const express = require('express');
const app = express();

const cors = require('cors');

app.use(cors())

app.get('/health', (req, res) => {
  res.send('OK');
});

module.exports = app;

Currently have out set to 60s, and a route like the health route shouldn't take that long.

Some interesting log entries

"Function execution took 60004 ms, finished with status: 'timeout'" 
textPayload: "Error: Retry total timeout exceeded before any response was received
    at repeat (/srv/functions/node_modules/google-gax/build/src/normalCalls/retries.js:80:31)
    at Timeout.setTimeout [as _onTimeout] (/srv/functions/node_modules/google-gax/build/src/normalCalls/retries.js:113:25)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)" 
9
  • Google Functions have a pretty slow cold start. What is the timeout of the function? Oct 7, 2019 at 20:16
  • It's 60 seconds. Oct 7, 2019 at 20:18
  • does it behave the same without cors? Oct 7, 2019 at 20:23
  • 1
    Is that your entire program? If you are timing out after 60 seconds, something else is going on. More details in your question, please. Include the logs from Stackdriver. Oct 7, 2019 at 20:29
  • 1
    Is the code posted your entire program? If not, update the question with all code or duplicate the problem with a small subset. As I previously commented, something else is going on or there are details that are not in your question. Oct 7, 2019 at 20:46

2 Answers 2

1

Cloud Function execution time is limited by the timeout duration, which you can specify at function deployment time. By default, a function times out after 1 minute. As it is stated in the official documentation:

When function execution exceeds the timeout, an error status is immediately returned to the caller. CPU resources used by the timed-out function instance are throttled and request processing may be immediately paused. Paused work may or may not proceed on subsequent requests, which can cause unexpected side effects.

Note that this period can be extended up to 9 minutes. In order to set the functions timeout limit you can use this gcloud command:

gcloud functions deploy FUNCTION_NAME --timeout=TIMEOUT FLAGS...

More details about your options could be found over here. But, maybe if your code takes a long time to execute, you may also consider using another serverless option, like Cloud Run.

2
  • 2
    The issue is really why it's taking so long and timing out, not the fact that it is timing out. The start time of the function shouldn't be over 60s. Oct 9, 2019 at 12:40
  • If this is all the code you are using and still facing the same issue you may consider opening a support ticket - cloud.google.com/support for a deeper investigation, as I reproduced your situation and I am not facing the issue you are facing. Oct 9, 2019 at 14:56
0

A Google Cloud Function can be thought of as the event handler for an incoming event request. A cloud function can be triggered from a REST request, pub/sub or cloud storage. For a REST request, consider the function that you supply as the one and only "handler" that the function offers.

The code that you supply (assuming Node.JS) is a function that is passedin an express request object and response object. In the body of the function, you are responsible for handling the request.

Specifically, your Cloud Function should not set up express or attempt to otherwise modify the environment. The Cloud Function provides the environment to be called externally and you provide the logic to be called. Everything else (scaling etc) is handled by Google.

2
  • 1
    Hi Kolban, thanks for your reply. I'm using express as the handler, I'm not attempting to modify the environment (I'm not using the app to listen to requests), just process the requests (because an express app essentially takes a request object and response object, just as the Google Functions expects. I'd understand if it didn't work everytime, but it's just not working on first invocation. Oct 7, 2019 at 20:19
  • For reference medium.com/google-cloud/… Oct 7, 2019 at 20:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.