php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61767 Shutdown functions not called in certain error situation
Submitted: 2012-04-18 18:39 UTC Modified: 2012-09-06 10:04 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: shiranai7 at hotmail dot com Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.0 OS: Win7
Private report: No CVE-ID: None
 [2012-04-18 18:39 UTC] shiranai7 at hotmail dot com
Description:
------------
I am using register_shutdown_function() toghether with error_get_last() and set_error_handler() to "handle" fatal errors (to display message in debug screen etc, details do not matter).

Everything works perfectly except for one specific scenario - If there is an error caused by calling a method on UNDEFINED (really undefined, not null etc) variable, then the following happens (NOT OK, UNEXPECTED):

 1. registered error_handler is invoked for E_NOTICE (undefined variable) 
 2. the error_handler throws an ErrorException
 3. the exception gets *eaten* by something, it never makes it out of the error_handler
 4. PHP exits with FATAL error (Call to a member function foo() on a non-object)
 5. No shutdown functions get called

----

If the variable is not UNDEFINED but just not an object (e.g. null), everything happens as follows (ALL OK, EXPECTED):

 1. PHP exits with FATAL error (Call to a member function foo() on a non-object)
 2. Registered shutdown functions are called

----

If I return TRUE from the error_handler, following happens (OK, BUT NOT ACCEPTABLE):

 1. registered error_handler is invoked for E_NOTICE (undefined variable) 
 2. the error_handler returns TRUE
 3. PHP exits with FATAL error (Call to a member function foo() on a non-object)
 4. Registered shutdown functions are called

Test script:
---------------
set_error_handler(function($code, $msg, $file = null, $line = null) {
    echo "Error handler called\n";
    throw new \ErrorException($msg, $code, 0, $file, $line);
});

register_shutdown_function(function(){
    echo "Shutting down\n";
    print_r(error_get_last());
});

// $undefined = null; // defined variable does not cause problems
$undefined->foo();

Expected result:
----------------
Fatal error: Call to a member function foo() on a non-object in _example_ on line 14
Shutting down Array ( [type] => 1 [message] => Call to a member function foo() on a non-object [file] => _example_ [line] => 14 ) 

Actual result:
--------------
Error handler called
Fatal error: Call to a member function foo() on a non-object in _example_ on line 13


Patches

bug61767.diff (last revision 2012-09-05 08:14 UTC by dmitry@php.net)
another_fix_for_bug61767.patch (last revision 2012-04-20 09:50 UTC by laruence@php.net)
bug61767.patch (last revision 2012-04-20 06:01 UTC by laruence@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-19 09:07 UTC] shiranai7 at hotmail dot com
Allow me to re-specify the expected result:
----------

Error handler called
Fatal error: Uncaught exception 'ErrorException' with message 'Undefined variable: undefined' in C:\Dokumenty\localhost\base\test_case1.php:14 Stack trace: #0 _example_(14): {closure}(8, 'Undefined varia...', 'C:\Dokumenty\lo...', 14, Array) #1 {main} thrown in C:\Dokumenty\localhost\base\test_case1.php on line 14
Shutting down Array ( [type] => 1 [message] => Uncaught exception 'ErrorException' with message 'Undefined variable: undefined' in _example_:14 Stack trace: ... [file] => _example_ [line] => 14 )

------
This is because the error_handler gets actually called for the E_NOTICE - so an exception should be thrown (and later uncaught in the example). But the exception gets *eaten* as I described.
 [2012-04-19 22:56 UTC] nikic@php.net
This seems related: https://bugs.php.net/bug.php?id=60909
 [2012-04-20 05:52 UTC] laruence@php.net
a E_NOTICE "undefined variable" has been triggered before the FATAL_ERROR.  hmm, 
the same thing as #60909
 [2012-04-20 06:01 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug61767.patch
Revision:   1334901670
URL:        https://bugs.php.net/patch-display.php?bug=61767&patch=bug61767.patch&revision=1334901670
 [2012-04-20 06:02 UTC] laruence@php.net
I attached a fix for this, maybe need dmitry to review it.
 [2012-04-20 06:02 UTC] laruence@php.net
-Status: Open +Status: Analyzed
 [2012-04-20 09:50 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: another_fix_for_bug61767.patch
Revision:   1334915414
URL:        https://bugs.php.net/patch-display.php?bug=61767&patch=another_fix_for_bug61767.patch&revision=1334915414
 [2012-04-24 11:37 UTC] shiranai7 at hotmail dot com
I wonder if this fix will make it to the 5.4.1 release.
 [2012-05-28 16:49 UTC] shiranai7 at hotmail dot com
Still the same (unexpected) result in 5.4.3.

Error handler called
Fatal error: Call to a member function foo() on a non-object in ... on line ...
 [2012-09-05 03:08 UTC] laruence@php.net
-Assigned To: +Assigned To: dmitry
 [2012-09-05 03:08 UTC] laruence@php.net
could you please look at this? 
since it is in shutdown pharse, then I think it's okey to suppress the exception?

thanks
 [2012-09-05 08:14 UTC] dmitry@php.net
The following patch has been added/updated:

Patch Name: bug61767.diff
Revision:   1346832893
URL:        https://bugs.php.net/patch-display.php?bug=61767&patch=bug61767.diff&revision=1346832893
 [2012-09-05 08:20 UTC] dmitry@php.net
I've added a patch that fixes the problem in different way. (bug61767.diff tested with 5.3 only)

In case of unhanded exception and following fatal error we just emit additional warning about uncaught exception before the fatal error. This solution doesn't miss exceptions (as another_fix_for_bug61767.patch) and doesn't require significant changes in VM (as bug61767.patch) that would affect PHP performance.

Please take a look.
 [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: Analyzed +Status: Closed
 [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.


 [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: Tue Dec 10 13:01:27 2024 UTC