note 129024 added to function.register-shutdown-function
| From: | josh at joshstrange dot com | Date: | Thu, 09 Nov 2023 16:27:53 +0000 |
| Subject: | note 129024 added to function.register-shutdown-function | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220980@lists.php.net to get a copy of this message | ||
> 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).
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 74.133.4.48)
----
Manual Page -- https://php.net/manual/en/function.register-shutdown-function.php
Edit -- https://main.php.net/note/edit/129024
Del: integrated -- https://main.php.net/note/delete/129024/integrated
Del: useless -- https://main.php.net/note/delete/129024/useless
Del: bad code -- https://main.php.net/note/delete/129024/bad+code
Del: spam -- https://main.php.net/note/delete/129024/spam
Del: non-english -- https://main.php.net/note/delete/129024/non-english
Del: in docs -- https://main.php.net/note/delete/129024/in+docs
Del: other reasons-- https://main.php.net/note/delete/129024
Reject -- https://main.php.net/note/reject/129024
Search -- https://main.php.net/manage/user-notes.php