.NET timeouts

Functions Framework for .NET uses cancellation tokens to indicate when a function times out.

You can retrieve the cancellation tokens based on how you implement your functions:

  • For HTTP functions implementing the IHttpFunction interface, retrieve the cancellation token from the HttpContext.RequestAborted property.

  • For event-driven functions implementing the ICloudEventFunction, ICloudEventFunction<TData> or ITypedFunction<TRequest, TResponse> interfaces, the cancellation token is passed into the HandleAsync method.

You use the cancellation token to handle a function timeout terminating the request. Pass the cancellation token to any asynchronous methods you call so that the function code aborts gracefully, rather than performing work that won't be used.

If your function uses time-consuming synchronous code and there's no asynchronous equivalent, periodically call the CancellationToken.ThrowIfCancellationRequested method; for example, in every iteration of a synchronous loop.