Shutdown callback is affected by max execution time, but documentation says otherwise.
Description
It is reproducible here: https://www.tehplayground.com/PwpR764URQYi79Lj
Resulted in this output:
Fatal error: Maximum execution time of 1 second exceeded in Standard input code on line 20
Start of shutdown
Fatal error: Maximum execution time of 1 second exceeded in Standard input code on line 37
But I expected this output instead:
Fatal error: Maximum execution time of 1 second exceeded in Standard input code on line 20
Start of shutdown
End of shutdown
As per the documentation we would expect the shutdown callback to not be affected by any limits set. As documented here: https://www.php.net/manual/en/function.register-shutdown-function.php#:~:text=to%20end%20cleanly.-,Note%3A,while%20a%20shutdown%20function%20is%20running%20it%20will%20not%20be%20terminated.,-See%20Also%20%C2%B6
This is a problem for my usecase where if there is a Fatal error , in this case due to timeout, we want to run the shutdown function and not run into a timeout.
PHP Version
PHP Latest
Operating System
No response
we want to run the shutdown function and not run into a timeout.
You can use set_time_limit(...) in shutdown function.
Yes we can use the set time limit in the shutdown function as a workaround , because the documented process does not work. So either the documentation is wrong or the php/c code that handles calling the shutdown function is wrong.
It would be beneficial if we can correct whichever is wrong so that devs in the future don't have to run into this inconsistency.
Having no timeout for shutdown functions would be bad if they block, as that could occupy all your workers and lead to DoS. I think the documentation should be adjusted instead.
Yes, this looks more like a doc issue
It should be clarified, that it will reset the time limit (as if set_time_limit were called) once "shutdown" starts
This means, this should be changed:
Additionally, if the max_execution_time runs out while a shutdown function is running it will not be terminated.
to:
Before the first shutdown function is executed, the execution time is reset to 0, just as if
set_time_limit( ini_get( 'max_execution_time' ) );were called.
@kkmuffme can you submit a PR? That would be helpful. :)
@kkmuffme I’m curious whether the max time is also reset during shutdown. For example let’s say a particular request raises or lowers its timeout with ini_set: does shutdown get the modified timeout or the original timeout?
@madelson
the modified timeout
It should be clarified, that it will reset the time limit (as if set_time_limit were called) once "shutdown" starts
Is it completely true though? In my tests, if you don't call set_time_limit in the shutdown handler, the handler will be affected only by hard_timeout.
If you do call set_time_limit in the shutdown handler, the timeout for handler will be the value of set_time_limit.