tags:

views:

703

answers:

5

Guys, on one page I am doing multiple AJAX calls. All calls return responses successfully but the last one (not related to other ajax calls) returns 500 internal server error as response code (as firebug tells). However, in spite of error code, correct content is returned from that AJAX call.

To my amazement, when I set display_errors option in php.ini as On, the error disappears and response in rendered on the page. I have setup error logging to a file but no error is logged corresponding to the above mentioned internal server error.

By the way, I am using Apache, JQuery, PHP5, APC (if it is relevant).

+2  A: 

That's very strange... Are you using some kind of library or other code that you didn't write that might be affecting the status code header independently of the actual returned value?

Gabriel Hurley
Yes, I am using a library called FlourishLib but I don't expect it to be the trouble maker as the same piece of software runs smoothly on another server
Paras Chopra
A: 

The PHP interpreter simply crashes during this one request. I know one potential reason that could crash PHP:

Due to some bugs in GCC 4.3, PHP compiled with this version of compiler has the implementation of exceptions broken. In some non-trivial cases, throwing an exception by the script causes the segfault and the script execution is terminated. The thesis was confirmed by PHP team a couple of months ago.

To verify, whether it happens to you, you can simply check, where the script execution crashes and if happens just after throwing an exception, you're at home. The check can be done by placing die() further and futher in the script and see, what happens. Another way is to use declare(ticks=1) and register a tick function that saves the last entry from debug_backtrace() to the file every tick, so that you'll get a report, how the script is executed.

Zyx
+2  A: 

The most likely answer, given the limited information available, is that PHP is reaching a fatal error after it has finished echoing your expected output.

To test: in the line that gets executed last, echo something. If that echo doesn't show up, then you know that your PHP script is halting somewhere. At that point, it's just a matter of debugging and tracing.

thedz
A: 

I've had somewhat similar problem and the issue was that the script was throwing exceptions and there was no catch block so the exception would be bubbled to the surface and fatal error "uncaught exception" happened. Which is standard php behaviour but on one particular server you would also get response code of 500 internal server error instead of 200 OK. Removing exceptions and replacing them with die() statements fixed the problem on that server (we could do that because it was simple script that really didn't benefit from exceptions in the first place)

brkonja
A: 

The problem is often fixed by ione of the following. Check the scripts permissions, rights and ownership. If returned after an ajax call, check whether there is nowhere a fatal error. Check whether you didn;t accidentally uplaoded the file to Unix in ASCII mode.

More info can be read at http://www.larshemel.com/forum/500_internal_server_error

Frits Schoenebeek