Execution after timeout

Sample that demonstrates code that should (but won't) execute after a GCF call times out.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <chrono>
#include <iostream>
#include <thread>

namespace gcf = ::google::cloud::functions;

gcf::HttpResponse concepts_after_timeout(gcf::HttpRequest request) {  // NOLINT
  using std::chrono::minutes;
  std::cout << "Function running..." << std::endl;
  if (request.verb() == "GET") std::this_thread::sleep_for(minutes(2));
  std::cout << "Function completed!" << std::endl;
  return gcf::HttpResponse{}.set_payload("Function completed!");


// Package tips contains tips for writing Cloud Functions in Go.
package tips

import (

// Timeout sleeps for 2 minutes and may time out before finishing.
func Timeout(w http.ResponseWriter, r *http.Request) {
	log.Println("Function execution started...")
	time.Sleep(2 * time.Minute)
	log.Println("Function completed!")
	fmt.Fprintln(w, "Function completed!")


package functions;

import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class AfterTimeout implements HttpFunction {
  private static final Logger logger = Logger.getLogger(AfterTimeout.class.getName());

  // Simple function to return "Hello World"
  public void service(HttpRequest request, HttpResponse response)
      throws IOException, InterruptedException {
    logger.info("Function running...");

    // May not execute if function's timeout is <2 minutes
    logger.info("Function completed!");
    BufferedWriter writer = response.getWriter();
    writer.write("Function completed!");


 * HTTP Cloud Function that may not completely
 * execute due to function execution timeout
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
exports.afterTimeout = (req, res) => {
  setTimeout(() => {
    // May not execute if function's timeout is <2 minutes
    console.log('Function running...');
  }, 120000); // 2 minute delay


def timeout(request):
    print('Function running...')

    # May not execute if function's timeout is <2 minutes
    print('Function completed!')
    return 'Function completed!'

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser