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.
{"cc_re__cc_re__cc_by_4_0": ["ta-position=\"footer\"\n  class=\"nocontent\"\n  \n  \n  \n    project-icon=\"https://www.gstatic.com/devrel-devsite/prod/v50a9748f336f0601961fb23638b43fbbc8f87f50f5f0aa83e2751247a72fbd3b/cloud/images/favicons/onecloud/super_cloud.png\"\n  \n  \n  feedback-type=\"thumb-rating\">\n\n  <button>\n  \n    \n    Send feedback\n  \n  </button>\n</devsite-feedback>\n       \n    \n    \n  \n\n  \n  \n</article>\n\n\n<devsite-content-footer class=\"nocontent\">\n  <p>Except as otherwise noted, the content of this page is licensed under the <a href=\"https://creativecommons.org/licenses/by/4.0/\">Creative Commons Attribution 4.0 License</a>, and code samples are licensed under the <a href=\"https://www.apache.org/licenses/LICENSE-2.0\">Apache 2.0 License</a>. For details, see the <a href=\"https://developers.google.com/site-policies\">Google Developers Site Policies</a>. Java is a registered trademark of Oracle and/or its affiliates.</p>\n  <p>Last updated 2024-02-29 UTC.</p>\n</devsite-content-footer>\n\n\n<devsite-notification\n>\n</devsite-notification>\n\n\n  \n<div class=\"devsite-content-data\">\n  "]}