Yesterday, on freenode #phpc, someone posted this curious error message:
PHP Fatal error: Maximum execution time of 0 seconds exceeded
Jokes aside of how, only eight hours into December, they had exceeded their monthly allotment of PHP time, this is a rather curious error message to receive: PHP’s set_time_limit()
function and associated max_execution_time
ini setting define a limit of 0 to mean no limit. A script with no limit should not be hitting a maximum execution time of zero seconds!
While several of us tried brainstorming possible causes, additional details were provided, including that the script, running on PHP 7 on unix, under php-fpm, was twice terminated after running for roughly two hours.
I set out to look at PHP’s source code, certain that there would be something that would shed light on this mystery.
Let’s investigate PHP internals
The first thing I did was search for “Maximum execution time” in the PHP sources, hoping to find exactly one hit. Ignoring test cases, that string appears exactly once in the source, in the zend_timeout()
function in zend_execute_API.c
.
When zend_timeout()
is called, PHP calls a function specified by the SAPI (the interface between PHP and the web server) if one is defined, and then emits an error and exits.
Knowing now where the error message is generated (and that it only comes from one place), I needed to determine what calls zend_timeout()
, so I searched the code for that function’s name. Excluding the function’s own definition, and its declaration in a header file, there were four results. Two of those results were specific to Windows support, and could be ignored.
The other two results were conveniently located next to each other in zend_set_timeout()
, both used as the callback function to a signal handler.
This means that, in normal use, the execution time limit message can only be generated in response to receiving a signal.
Signals
In UNIX-like systems, signals provide for a limited form of inter-process communication, in the form of sending and receiving an interrupt with a numeric identifier. The Wikipedia page on signals provides additional detail.
There C standard defines six signals, but many operating systems define and use many additional signals.
Some signals sent to a process cause its unconditional termination (e.g. SIGKILL
) or have other operating-system level effects (such as SIGSTOP
and SIGCONT
). Most other signals can be caught by the target process if it has installed a signal handler to receive that notification. (If a signal handler is not installed, a default handler is used, which often, but not always, results in the self-termination of the process.)
How PHP’s execution time limit works
So, then, what signal triggers zend_timeout()
? A few lines up indicates the answer: SIGPROF
. (Alternatively, SIGALRM
is used when running under Cygwin on Windows.)
SIGPROF
was a new one to me. Wikipedia says:
The
SIGALRM
,SIGVTALRM
andSIGPROF
signal is sent to a process when the time limit specified in a call to a preceding alarm setting function (such assetitimer
) elapses.SIGALRM
is sent when real or clock time elapses.SIGVTALRM
is sent when CPU time used by the process elapses.SIGPROF
is sent when CPU time used by the process and by the system on behalf of the process elapses.
And indeed, there is a call to setitimer()
before PHP installs the signal handler on SIGPROF
. The man
page for setitimer()
describes how to use it to set a timer that counts time the process spends executing, triggering a SIGPROF
after the timer elapses.
Some more searching through PHP’s code makes clear how the entire process works: when PHP is loaded, set_time_limit()
is called, or max_execution_time
is changed, PHP clears the timer (if set), and re-sets the timer (if the timeout is non-zero). Additionally, during request start-up, the signal handler on SIGPROF
is installed (regardless of whether a timeout is actually set).
When the timer set by setitimer
fires, the SIGPROF
handler, zend_timeout()
runs, displays the error message with the number of seconds filled in from the current configuration, and exits.
Reading between the lines
That answers the question of how the execution time limit error was displayed. But it doesn’t answer why: one would naturally assume that since the timer isn’t set, the operating system won’t send SIGPROF
, and the script wouldn’t be terminated.
The answer lies in one further detail of the UNIX signal mechanism: any process can send any other process any signal (except where disallowed by security policy, such as for processes owned by a different user).
This means that a script doesn’t actually need to hit the time limit to be killed. PHP just needs to be told that it has.
You can test this yourself by doing something similar to this:
$ php -r 'posix_kill(getmypid(), SIGPROF);'
Fatal error: Maximum execution time of 0 seconds exceeded in Command line code on line 1
So, what really happened?
Unfortunately, a signal does not come with the identity of its source. Were that the case, it would have been easy to determine what was killing the process and what configuration to change to stop it. In this case, the resolution was to modify the php code to “only” take 20 minutes to run, so the timeout was no longer an issue.
If we make the assumption that there wasn’t a malicious user on the server sending unwanted signals as a denial of service attack, the documentation for max_execution_time
hints at one possibility:
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.
With some further research, I found that neither Apache nor nginx explicitly send SIGPROF
, though. And while nginx does use setitimer
, its use triggers a SIGALRM
.
My best guess is that there likely was some sort of watchdog process on the server that killed off the process running php after it consumed too many resources (either memory or time).
It's probably a bug (or at least, undesirable confusion) in PHP that a SIGPROF
that doesn’t arise strictly from a timer expiration displays the same message as if the timer did expire, but this looks to be correctable.