I have a PHP script which takes a long time and makes many DB connections.
After about 5 minutes the server sends me an error 500 and the error_log shows the following :
(104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
(104)Connection reset by peer: mod_fcgid: ap_pass_brigade failed in handle_request_ipc function
I googled this and found that the obvious answer is to change either the "PHP_FCGI_MAX_REQUESTS" variable inside the "fcgi-bin/php5.fcgi" file for the domain or "FcgidMaxRequestsPerProcess" variable inside the Apache config file, but I can't do either of these, as the server hosts multiple websites, all running with FastCGI.
I tried to catch the error 500 and redirect the page with the following PHP code :
register_shutdown_function('rerun');
$rerun = isset($_GET['rerun']) ? true : false;
main($rerun);
function main ($rerun=false) {
// Lots and lots of stuff
}
function rerun() {
if (error_get_last() != NULL) {
header('Location: http://www.example.com/myscript.php?rerun');
}
}
But it doesn't work either, I still end up with an error 500.
Does anyone have any idea on how to fix this ? Either by declaring the FastCGI variables locally (i.e. for this website only), or, preferably, by correctly catching the error 500 and triggering the "rerun" function before it all crashes down.
I think it's because your script doesn't send any data to apache. And you got this error because of timeout of awaiting response by apache from fast-cgi. I have similar issue on nginx+php-fpm. I've solved it with this workaround:
function explicitBufferFlush()
{
echo str_repeat('*', 1024 * 64);
flush();
ob_flush();
}
What it does it simply send *
to output, and while your script is running apache recieve data and don't drop connection. You can call it on every iteration of some heavy loop or anything else. Or you can try to set FcgidIOTimeout directive to some high value in your .htaccess
I'm sorry, just coming across this question so long after it was asked, you have probably solved this already, but since I have encountered this before, I thought I would throw this out there for anyone who is searching...
Typically, the FastCGI process has a timeout of 5 minutes. Most likely you are getting the error because FastCGI is aborting at 5 minutes, which prevents it from sending any output to Apache. Try increasing the timeout to 10 minutes, or drastically reducing the processing being done, to prove that this is the case.
The FastCGI process timeout is different from the PHP timeout in that it is a simple calculation of the time since the process started, as opposed to time in the process, which is what the PHP timeout is based on. (In other words, time spent waiting for a database response does not count toward the PHP timeout, but it does count against the FastCGI timeout. So, even if PHP has a shorter timeout set, you can still hit the FastCGI timeout first.)