php.net |  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
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (100.0%)
From: tyrael@php.net Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.0RC6 OS: linux
Private report: No CVE-ID: None
 [2012-01-27 18:07 UTC] tyrael@php.net
Description:
------------
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!!!" 
printed.
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.

wtf?

Test script:
---------------
<?php

register_shutdown_function(function(){echo("\n\n!!!shutdown!!!\n\n");});
set_error_handler(function($errno, $errstr, $errfile, $errline){throw new Exception("Foo");});

require 'notfound.php';



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-09 14:33 UTC] jpauli@php.net
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] nikic@php.net
Together with https://bugs.php.net/bug.php?id=61767 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] nikic@php.net
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 http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_execute_API.c#775).
 [2012-04-20 00:15 UTC] nikic@php.net
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] bishop@php.net
@nikic: I can generate the same path without the first non-fatal error:

----------------------------------------------------------------------

register_shutdown_function(function(){echo("\n\n!!!shutdown!!!\n\n");});
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] dmitry@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=b29dc146b9311c14186c14bcb1c8ae5288b65d73
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] dmitry@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: dmitry
 [2012-09-06 10:04 UTC] dmitry@php.net
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
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

The same as #61767.
 [2014-10-07 23:22 UTC] stas@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=b29dc146b9311c14186c14bcb1c8ae5288b65d73
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)
 [2014-10-07 23:33 UTC] stas@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=b29dc146b9311c14186c14bcb1c8ae5288b65d73
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)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC