doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

Shutdown callback is affected by max execution time, but documentation says otherwise.

Open Serchman opened this issue 2 years ago • 6 comments

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

Serchman avatar Aug 14 '23 12:08 Serchman

we want to run the shutdown function and not run into a timeout.

You can use set_time_limit(...) in shutdown function.

KapitanOczywisty avatar Aug 14 '23 15:08 KapitanOczywisty

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.

Serchman avatar Aug 14 '23 16:08 Serchman

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.

iluuu1994 avatar Aug 14 '23 17:08 iluuu1994

Yes, this looks more like a doc issue

Girgias avatar Aug 14 '23 17:08 Girgias

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 avatar Jan 16 '25 22:01 kkmuffme

@kkmuffme can you submit a PR? That would be helpful. :)

Girgias avatar Jan 27 '25 14:01 Girgias

@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 avatar Mar 27 '25 09:03 madelson

@madelson

the modified timeout

kkmuffme avatar Mar 27 '25 15:03 kkmuffme

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.

asavartsov avatar Sep 12 '25 12:09 asavartsov