The problem: setting max_execution_time / using set_time_limit() I don’t get the wanted results
If you are here, you are probably looking for an answer to the question:
why set_time_limit() is not working properly and the script is killed before?
why ini_set(‘max_execution_time’, X), is not working properly and the script is killed before?
The answer is not exactly one, because there are many variables to consider.
The concepts: PHP itself vs FPM
Anyway, I will list basic concepts that will help to find the best path to your answer.
The first, brutal, note is: directives you specify in php-fpm.conf are mostly not changeable from ini_set().
Please also note that set_time_limit() is a almost nothing more than a convenience wrapper on ini_set() in the background, so the same concept applies.
For simplicity, in this post I will talk about set_time_limit(), but the same applies to ini_set(whatever_related_to_time).
According to what we already said, it’s important to understand that set_time_limit() can only “overwrite” the max_execution_time directive, because
they only affect the execution time of the script itself
(there is a specific note in the official documentation).
On the other hand, directives like request_terminate_timeout are related to FPM, so we are talking about the process management level here:
should be used when the ‘max_execution_time’ ini option does not stop script execution for some reason.
Web server dependent directives
What we have seen so far aren’t the only variables in the game.
You should check your web server configuration, too, to be sure nothing is playing a role in your timeout problem.
A very common example is the fastcgi_read_timeout, defined as:
a timeout for reading a response from the FastCGI server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the FastCGI server does not transmit anything within this time, the connection is closed.
So…what to do?
The basic answer is: have perfectly clear what was defined by web server (ex.: fastcgi_read_timeout), what defined by FPM (ex.: request_terminate_timeout) and what was defined by PHP itself (ex.: max_execution_time).
For example, if request_terminate_timeout is set to 1 second, but max_execution_time is set to 2 seconds, the script will end after 1 second (so, whichever comes first, even if you have set max_execution_time with ini_set() in your script) .
A possible solution is simply using max_execution_time in place of request_terminate_timeout (but use it, too, to define a solid maximum upper-bound).
The max_execution_time description in the official documentation has an hint about this common problem, that is:
Your web server can have other timeout configurations that may also interrupt PHP execution. Apache has a Timeout directive and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific details.
Any hints to share?
0 Comments Leave a comment