Function timeout

If a function runs for too long, the system takes steps to terminate or throttle it. This timeout period defaults to 60 seconds, but you can extend or reduce it when you deploy the function:

  • In Cloud Functions (1st gen), the maximum timeout duration is nine minutes (540 seconds).
  • In Cloud Functions (2nd gen), the maximum timeout duration is 60 minutes (3600 seconds) for HTTP functions and 9 minutes (540 seconds) for event-driven functions.

When function execution reaches its timeout, the system response depends on whether it's a 1st gen or 2nd gen function.

  • 1st gen: A response message with an HTTP error status, generally 408, is immediately returned to the caller; function execution is halted.
  • 2nd gen: A response message with an HTTP 504 error status is immediately returned to the caller. The function instance might be throttled, but it continues operating until it exits on its own. Any response message that the function generates is discarded and not returned to the caller.

This behavior of 2nd gen functions can cause unexpected side effects. A common symptom is the appearance of work and logs from one request "leaking" into a subsequent request. To avoid this, prevent timeouts in your functions with the following techniques:

  1. Set a timeout higher than your expected function execution time.
  2. Track the amount of time left during execution. Then perform cleanup and return early.

Set a timeout duration

You can set a function's timeout duration at deployment using the Google Cloud CLI or the Google Cloud console.

gcloud

If you are deploying using the gcloud CLI, use the --timeout flag:

gcloud functions deploy YOUR_FUNCTION_NAME --timeout=TIMEOUT_DURATION ...

To edit an existing timeout duration with Google Cloud CLI, redeploy the function with a new timeout value.

Console

To set the timeout duration during function creation in the Google Cloud console:

  1. Go to the Cloud Functions Overview page in the Google Cloud console.
  2. Click Create function.
  3. Fill in the required fields for your function.
  4. Expand the Runtime, build... section at the end of the page and click the Runtime tab.
  5. In the Timeout field, enter a number of seconds.

    To edit an existing timeout duration in Google Cloud console, on the functions overview page click the name of the function to go to its details page. On the details page click Edit, expand the Runtime, build... section, and click the Runtime tab, where you can directly edit the value in the Timeout field.