4
votes

In the Google Cloud Functions documentation under the Time Limits section, it says that the max duration should be 540 seconds. After that the cloud function should terminate.

"Max function duration - The maximum amount of time a function can run before it's forcibly terminated - 540 seconds"

I've ran some tests on http-triggers and noticed that if a response is not sent back within 6 seconds the function terminates.

I also ran another test that sent back a response immediately and asynchronously logs a message every minute.

Currently after 40 minutes, it's still logging a message.

Here is the code snippet:

function recursiveCallback (n) {
    console.log(`MarkTest: Callback Pass ${n}`);
    setTimeout(function () {
        recursiveCallback(n + 1);
    }, 1000 * 60);
}

exports.test_func = function (req, resp, callback) {
    console.log('Sending Response. response');
    resp.send('');
    console.log('Response Sent.');
    console.log('Starting Async Polling');
    recursiveCallback(0);
    console.log('test_func End')
};

Is this a bug, or is this intentional?

1
Being able to continue running your function after the max duration seems to be a bug. To be safe, you should adhere to your specified limit. Nathan has discovered the same behavior as you and has reported it to Google. See his post for more details. blog.liftsecurity.io/2017/04/11/… - Kevin Lee
From my experience, http-triggered functions don't terminate after 6 seconds. If this happens again, consider filing a bug report. - Kevin Lee

1 Answers

2
votes

1) I haven't seen the 6 second HTTP timeout, can you provide any steps to reproduce?

2) Cloud Functions currently (Feb 2018) doesn't forcefully stop functions after the timeout has elapsed if there are still async tasks in flight; the CPU throttles down but the function may run for an (undefined) amount of time. The original idea was to let minor cleanup/background work finish before stopping the function. However, we're likely to change this behavior to stop the function at timeout, as the current behavior can cause confusion for developers, since it's not guaranteed or documented how much longer the function can run, and under what circumstances.