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. 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, 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 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:
- Set a timeout higher than your expected function execution time.
- 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:
- Go to the Cloud Run functions Overview page in the Google Cloud console.
- Click Create function.
- Fill in the required fields for your function.
- Expand the Runtime, build... section at the end of the page and click the Runtime tab.
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.