FastCGI scripts won't run reliably

software development


I’m trying to port my Perl .cgi scripts to FastCgi (.fcgi), but they won’t run reliably. Sometimes they load in a browser, often they don’t, and when they don’t the browser spends a couple minutes trying to load the page before timing out and giving an Internal Server Error. That long timeout makes it hard to troubleshoot.

As an example, I have a simple file, <ad.fcgi>. I just loaded it in a browser successfully. I hit Reload, it loaded again just fine. I hit Reload again, and this third time, I get the interminable trying-to-load-the-page-with-the-eventual-timeout. The error in the logfile says:

[Thu Nov 16 12:29:06 2006] [error] [client] FastCGI: comm with (dynamic) server “/home/user/” aborted: (first read) idle timeout (120 sec)
[Thu Nov 16 12:29:06 2006] [error] [client] FastCGI: incomplete headers (0 bytes) received from server “/home/user/”

The scripts always run fine from the command line. And of course, the permissions of the file and the enclosing folder are set to 755, and I’m printing the Content-type header. If those things weren’t true, it would never run.

I’ve written to Support several times but they won’t help, and referred me to the forums.


Okay, I figured out the problem. If you use a command like “exit;” in Perl it doesn’t just stop the script, requiring it to be restarted from scratch the next time it’s run (losing the advantage of running certain code only once), oh no, that’s not all, it also MAKES THE SCRIPT COMPLETELY UNRELIABLE for future calls.

The solution is to use the obscure goto() command in Perl instead of exit. For example:

[quote][code]use FCGI;
while ( FCGI::accept() >= 0 ) {
print “Content-type:text/html\n\n”;

if (some test) { goto(EXITLABEL);}

more code;

I overhauled the Wiki entry on FastCGI to try to save others a lot of grief. This one took me months to solve, off and on, with nothing helpful from Support.