Random PHP FastCGI / Connection reset by peer / incomplete headers

I’ve been having random 500 Internal Server errors on my PHP / MySQL based sites on various shared hosts. I’m using PHP 5.2.17 through CGI/FastCGI on a shared Linus server. When I look in the logs, I see this:

[error] [client 75.71.176.224] (104)Connection reset by peer: FastCGI: comm with server "/dev/shm/blackmou-php.fcgi" aborted: read failed, referer: ...
[error] [client 75.71.176.224] FastCGI: incomplete headers (0 bytes) received from server "/dev/shm/blackmou-php.fcgi", referer: ...
[error] [client 75.71.176.224] (104)Connection reset by peer: FastCGI: comm with server "/dev/shm/blackmou-php.fcgi" aborted: read failed, referer: ...
[error] [client 75.71.176.224] FastCGI: incomplete headers (0 bytes) received from server "/dev/shm/blackmou-php.fcgi", referer: ...

Anyone know how to resolve this?

a stab in the dark: Do you have any exit()s in any of your code?

… or any die()s ?

See http://stackoverflow.com/questions/6805474/errors-when-calling-exit-function-for-fastcgi for eg.

Thanks for the response. I’m using “exit()” only a couple times across thousands of lines of code and use “die()” mainly in relation to testing (which I then comment out), or for failing on a mysql connection such as…

mysql_select_db($database_pdb_conn, $pdb_conn);
$ResultUpdate1 = mysql_query($insertUpdateSQL, $pdb_conn) or die(mysql_error());

Any other ideas?

Anyone have any other ideas?

Hi Adam,

A quick google search suggests that the errors might be caused by a DB connection problem. It’s probably worth double-checking the connection details for the DB server with your hosting company, to make sure that the hostname and port are being set correctly etc.

I was fighting this error that popped up sporatically. Basically, it can occur when your CGI script (in my case PHP) does not return a complete set of headers (ie. script crashes or executes an exit() or die() function). I was able to track this by changing PHP (modifying php.ini) to log all errors/warnings to a file (see error_log and log_errors PHP ini settings).

The specific issue (and why it occurred randomly for me) was that PHP was running out of memory. I had to up the “memory_limit” option in my php.ini file, and everything worked without issue. The key was logging the error though first so I knew what was causing PHP to crash.