php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #48969 register_shutdown_function() buggy in combination with set_error_handler()
Submitted: 2009-07-18 16:14 UTC Modified: 2010-01-13 02:11 UTC
From: bschussek at gmail dot com Assigned: kalle (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.2.10 OS: Linux Ubuntu 9.04
Private report: No CVE-ID: None
 [2009-07-18 16:14 UTC] bschussek at gmail dot com
Description:
------------
I registered both a custom error handler and a custom shutdown function. When calling require with an invalid filename, the following happens:

1. The "warning" error triggers the handler
2. The "fatal" error fires
3. The shutdown function is called

BUT when the error handler throws an exception, no shutdown function is called. This DOES work when using trigger_error() instead of throwing the exception.

Reproduce code:
---------------
function error_handler()
{
  var_dump('ERROR');
  throw new Exception('my exception');
}
set_error_handler('error_handler');

function shutdown()
{
  var_dump('SHUTDOWN');
}
register_shutdown_function('shutdown');

require 'foobar.php';

Expected result:
----------------
string(5) "ERROR"

Fatal error: Uncaught exception 'Exception' with message 'my exception'

string(8) "SHUTDOWN"

Actual result:
--------------
string(5) "ERROR"

Fatal error: main(): Failed opening required 'foobar.php'



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-20 10:57 UTC] jani@php.net
FATAL errors are fatal, the execution of the scripts ends at those. No bug here.
 [2009-07-20 11:12 UTC] derick@php.net
I am reopening this. Because even with fatal errors, the shutdown handler should run. It always has so if that changes it is a BC break. Assigning to Dmitry.
 [2009-07-20 11:43 UTC] bschussek at gmail dot com
In reply to Jani:

Of course fatal errors are fatal, but my registered error handler should run after the warning, BEFORE the fatal error. If you replace the code

throw new Exception('my exception');

with

trigger_error(...);

instead, you will see that the custom error is triggered correctly. The same is not the case for exceptions.
 [2009-07-21 12:43 UTC] jani@php.net
It's not really buggy, you just can't call user functions when there are uncatched exceptions thrown. Comment in sources say it would otherwise cause instable executor. So instead of crashing, it just won't run your function. :)
 [2009-07-21 12:46 UTC] jani@php.net
See also bug #30266 for which this fix was added. Commit log mentions a possibility of throwing E_FATAL error for this but leaves that to be discussed. I guess it was never discussed. :)
 [2009-07-21 12:50 UTC] jani@php.net
One more thing: The shutdown func WILL be run. As long as there are no uncaught exceptions left.
 [2009-07-21 12:58 UTC] jani@php.net
This is really documentation issue. You can't call _any_ user callbacks (f.e. using call_user_func() & such) when there are uncaught exceptions.
 [2009-07-21 13:25 UTC] bschussek at gmail dot com
I still don't quite understand this. Shouldn't my above code behave the same way as this code?

Code:
-----
function shutdown()
{
  var_dump('SHUTDOWN');
}
register_shutdown_function('shutdown');

throw new Exception('my exception');

Output:
-------
Exception: my exception in ...

string(8) "SHUTDOWN"


The only code difference is that in the original code the exception was thrown from within the error handler. AND that in the original code the shutdown function was not called.)
 [2010-01-13 02:09 UTC] svn@php.net
Automatic comment from SVN on behalf of kalle
Revision: http://svn.php.net/viewvc/?view=revision&revision=293478
Log: Fixed bug #48969 (register_shutdown_function() buggy in combination with set_error_handler())
 [2010-01-13 02:11 UTC] kalle@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2012-07-17 02:24 UTC] josh at jj88 dot org
See https://bugs.php.net/bug.php?id=60909 and https://bugs.php.net/bug.php?
id=61767 for additional information.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Jul 17 22:01:26 2019 UTC