3

I'm trying to gracefully catch a timeout in my script using register_shutdown_function() in php. Here´s my code:

function catchTimeout() {
    echo "Caught timeout!";
    // Do some logging, error handling here
}

register_shutdown_function("catchTimeout");
set_time_limit(2);
echo "Working...";
$i = 0;
while(1) {
    $i++;
}

I would expect this script to echo "Working...", run for two seconds and then echo "Caught timeout!". This is not the case, instead I see the fatal error Message: Maximum execution time of 2 seconds exceeded. The error was not caught, and my catchTimeout() was not executed.

This forum post (https://bytes.com/topic/php/answers/166258-register_shutdown_function-timeouts) tells me it is possible to catch timeouts, but you just don't see any echos, but I couldn't do anything else either.

Mid 2002 this bug was reported (https://bugs.php.net/bug.php?id=17461), quote from the comments:

REGISTER_SHUTDOWN_FUNCTION() is suppose to kick-off when any one of the following things happen -- Exit, Error, TIMEOUT or User Abort. According to Rasmus, the TIMEOUT functionality of this function works fine under Linux -- however, it's not working under Win32 !

However, it doesn't work on my Linux machine (Apache2, php 5.6.4). I saw that the bug was posted earlier in 2001 (https://bugs.php.net/bug.php?id=14542) and was 'fixed' in 2003.

Has anyone come across the same problem, and hopefully a solution? After a long search, no answer was found.

Inigo
  • 331
  • 3
  • 12
  • 3
    Possible duplicate of [How to catch the fatal error: Maximum execution time of 30 seconds exceeded in PHP](http://stackoverflow.com/questions/6861033/how-to-catch-the-fatal-error-maximum-execution-time-of-30-seconds-exceeded-in-p) – fusion3k Mar 16 '16 at 16:33
  • Your shut-down function probably still runs, you just don't see the echoed result because it's being overridden by the fatal error message. Try dumping something to a file on shut-down to verify this. – apokryfos Mar 16 '16 at 16:52
  • 1
    Possible duplicate, but there is still no solution to the problem. I've tried to write text to a file, which failed. I've tried that, echoing, redirecting.. There is no way to test if the code was executed. – Inigo Mar 16 '16 at 16:59

0 Answers0