|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60909 custom error handler throwing Exception + fatal error = no shutdown function
Submitted: 2012-01-27 18:07 UTC Modified: 2012-09-06 10:04 UTC
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (100.0%)
From: Assigned: dmitry
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.0RC6 OS: linux
Private report: No CVE-ID:
 [2012-01-27 18:07 UTC]
first of all sorry for the weird Summary, I couldn't come up with a better one.
it is weird.

so it seems that if you have a shutdown function callback set and a custom error 
handler which would throw and exception and you happen to have a fatal error, 
the shutdown function won't be called. 

See the attached script, the error handler shouldn't really do anything here, 
because it won't be called for the fatals, but somehow it still screw things up.

If I remove the error handler, I will see the "!!!shutdown!!!" printed.
If I put a "return false;" before the throw I will see the "!!!shutdown!!!" 
If I add E_NOTICE as the $error_type to the set_error_handler call I will see 
the "!!!shutdown!!!" printed.
If I add E_WARNING as the $error_type to the set_error_handler call I will NOT 
see the "!!!shutdown!!!" printed.


Test script:

set_error_handler(function($errno, $errstr, $errfile, $errline){throw new Exception("Foo");});

require 'notfound.php';


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-09 14:33 UTC]
Ok I can reproduce.

Try replacing your require (which generates E_COMPILE_ERROR) by an unknown 
function call (which generates E_ERROR), and all's just fine

So there is a trick in the error engine, any kind of fatal shouldn't be handled 
by user defined error handlers, here we got proof it's not the case

Tip: Reproduced on 5.3.10 as well
 [2012-04-19 23:06 UTC]
Together with it is a bit more clear under which circumstances this occurs:

1. A non-fatal error is thrown
2. The error handler throws an Exception
3. A fatal error is thrown

In this particular case:

1. require() throws an E_WARNING (Warning: require(notfound.php): failed to open stream: No such file or directory).
2. The error handler throws the Exception
3. require() throws an E_COMPILE_ERROR (Fatal error: require(): Failed opening required 'notfound.php')
 [2012-04-19 23:40 UTC]
So, this is what I think is happening here:

1. The first non-fatal error (here warning) throws an Exception, i.e. sets EG(exception)
2. The second fatal error then causes a zend_bailout() moving us directly to php_request_shutdown()
3. During the shutdown sequence PHP will try to call the shutdown handler using call_user_function().
4. As EG(exception) is still set, the call is not allows (see
 [2012-04-20 00:15 UTC]
I tried adding an EG(exception) = NULL; at the start of php_request_shutdown() and it indeed fixes the issue. Though probably that's not the right way to fix this.
 [2012-09-04 16:24 UTC]
@nikic: I can generate the same path without the first non-fatal error:


set_error_handler(function($errno, $errstr, $errfile, $errline){throw new Exception("Foo");});

class Bad {
    public function __toString() {
        throw new Exception('Oops, I cannot do this');

$bad = new Bad();
echo "$bad";


This is on 5.3.10, like @jpauli report, and 5.4.6 as well.

The @laruence EG(exception) = NULL patch handles this case as well.  AFAIK, that patch is appropriate.
 [2012-09-06 10:04 UTC]
Automatic comment on behalf of
Log: - Fixed bug #61767 (Shutdown functions not called in certain error situation) - Fixed bug #60909 (custom error handler throwing Exception + fatal error = no shutdown function)
 [2012-09-06 10:04 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: dmitry
 [2012-09-06 10:04 UTC]
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at

 For Windows:
Thank you for the report, and for helping us make PHP better.

The same as #61767.
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 25 07:02:14 2014 UTC