update page now
Longhorn PHP 2026 - Call For Papers

Voting

: eight minus three?
(Example: nine)

The Note You're Voting On

josh at joshstrange dot com
2 years ago
> Shutdown functions run separately from the time tracked by max_execution_time. That means even if a process is terminated for running too long, shutdown functions will still be called. Additionally, if the max_execution_time runs out while a shutdown function is running it will not be terminated.

This does not appear to be true in our testing, specifically "Additionally, if the max_execution_time runs out while a shutdown function is running it will not be terminated".  For example:

<?php
set_time_limit(5);

register_shutdown_function(function() {
    $start = time();
    for($i = 0; $i < 40; $i++) {
        echo "Run 1: $i\n";
        while(1) {
            $elapsed = time() - $start;
            if($elapsed > $i) {
                break;
            }
        }
    }
});

?>

This will print out:

Run 1: 0
Run 1: 1
Run 1: 2
Run 1: 3
Run 1: 4
Run 1: 5

Then it will die with:

Execution took longer than 5 seconds, sent SIGTERM and terminated

If you register multiple shutdown functions and an earlier one exceeds the execution time the later ones will _not_ be run. 

If you need your shutdowns to have unlimited time (like the docs suggest it works) one solution might be to register a shutdown function like this early in your code:

<?php
register_shutdown_function(function() {
    set_time_limit(0);
});
?>

So that you give yourself unlimited time in your subsequent shutdown functions.

See an example here: https://www.tehplayground.com/wJLtMi3Z5c1sTi9Y (Note, 5 seconds is the max you can set on this website. If you remove the first shutdown function you it will be killed after 3 seconds).

<< Back to user notes page

To Top