Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom exception handler cannot use exit or die #4055

Closed
Max-Sum opened this issue Oct 24, 2014 · 16 comments
Closed

custom exception handler cannot use exit or die #4055

Max-Sum opened this issue Oct 24, 2014 · 16 comments

Comments

@Max-Sum
Copy link

Max-Sum commented Oct 24, 2014

example:

<?php
set_exception_handler("custom_exception_handler");
function custom_exception_handler($e){
    echo "foo";
    exit;
}    

throw new Exception("bar");
?>

will throw

        exception 'Exception' with message 'bar' in *****:8 Stack trace: #0 {main}
@Max-Sum Max-Sum changed the title custom error handler cannot use exit or die custom exception handler cannot use exit or die Oct 24, 2014
@staabm
Copy link
Contributor

staabm commented Oct 24, 2014

Your example is consistent with php, see http://3v4l.org/SgHf0

Maybe your sample should use set_exception_handler instead?

@Max-Sum
Copy link
Author

Max-Sum commented Oct 24, 2014

Yeah, I just make a mistake

@staabm
Copy link
Contributor

staabm commented Oct 24, 2014

I see, now we have something to reproduce the incompatibility

http://3v4l.org/quVna

@paulbiss
Copy link
Contributor

This is a KP. You need to set Eval.AlwaysLogUnhandledExceptions=false to get the same behavior as PHP 5. See #3019.

@Max-Sum
Copy link
Author

Max-Sum commented Oct 25, 2014

If you mean hhvm.log.always_log_unhandled_exceptions, I have set it to false.

@paulbiss
Copy link
Contributor

I'll dig into it.

@paulbiss paulbiss self-assigned this Oct 26, 2014
@Majkl578
Copy link
Contributor

@Max-Sum:

$ hhvm -d hhvm.log.always_log_unhandled_exceptions=false test.php

works as expected (= zend) for me on master, it just outputs foo.

@Max-Sum
Copy link
Author

Max-Sum commented Oct 27, 2014

But I got

Fatal error: Uncaught exception 'Exception' with message 'bar' in *****test.php:8
Stack trace:
#0 {main}

with that command.

@paulbiss
Copy link
Contributor

@Max-Sum I can't repro either

$ cat test.php
<?php
set_exception_handler("custom_exception_handler");
function custom_exception_handler($e){
    echo "foo\n";
    exit;
}

throw new Exception("bar");

$ hhvm -d hhvm.log.always_log_unhandled_exceptions=false test.php
foo

@Max-Sum
Copy link
Author

Max-Sum commented Oct 27, 2014

Well I can get the right result now from cli, but I still get wrong one from fast-cgi.
But I have set hhvm.log.always_log_unhandled_exceptions = false in /etc/hhvm/php.ini.

hhvm-bot pushed a commit that referenced this issue Oct 30, 2014
…bleHipHopSyntax

Summary: By turning these off by default, we should match Zend behavior better on fatals with custom error handlers.

Few examples:
```
set_error_handler(function () { echo 'hello'; });
invalid();
```
This should produce only a fatal error message without calling user handler.

```
set_error_handler(function () { echo 'hello'; });
echo $invalid;
```
This should call user handler without producing notice message.

```
set_exception_handler(function () { echo 'hello'; });
throw new Exception();
```
This should call user handler without producing fatal error message.

Relates to #3019 and #4055.
Closes #4066

Reviewed By: @ptarjan

Differential Revision: D1640294

Signature: t1:1640294:1414638838:69aa654fbc4b805b3f1731d786a4e0ad338d29aa
@paulbiss
Copy link
Contributor

I just ran this on a fastcgi server, I'm not seeing anything in my error log, looks fine to me. Could you provide more details about what you expect to see?

@Morik
Copy link

Morik commented Dec 26, 2014

Yesterday i'll run into the same problem.
Using Debian 7 with nginx 1.6.2 and hhvm 3.4.2 in fastCGI mode it is easy to reproduce the error.
Setting hhvm.log.always_log_unhandled_exceptions=false makes no real difference, because it will only clean up the log.

Create 2 testfiles:

<?php

set_exception_handler (function ($e){
    echo 'Exception thrown...';
});

throw new Exception('Error');
?>

First one you get exactly what you expect: Return is just "Exception thrown..."
If you use istead of this the following code:

<?php

set_exception_handler (function ($e){
    echo 'exception thrown...';
    exit();
});

throw new Exception('Error');
?>

the result is an empty page.
If you register an additional shutdown function it will be called only in the first example.

Using instead the CLI it works as expected.

@paulbiss
Copy link
Contributor

paulbiss commented Jan 5, 2015

I believe this is due to a lack of implicit flush, you could try hhvm.server.implicit_flush=true

@Morik
Copy link

Morik commented Jan 6, 2015

Seams to work.
But won't this worse the performance ?

zsxsoft referenced this issue in zblogcn/zblogphp Feb 4, 2015
sandeepone added a commit to gleez/cms that referenced this issue Mar 21, 2015
@colin-kiegel
Copy link

implicit_flush is the right direction. If you flush() before exit() HHVM behaves the same as PHP. Interestingly this issue only occurs in callbacks registered with set_exception_handlers - not in regular code.

<?php
set_exception_handler("custom_exception_handler");
function custom_exception_handler($e){
    echo "I need to get flushed!";
    flush(); // happens implicitly with PHP, but not with HHVM.
    exit;
}    

throw new Exception("bar");
?>

Note the difference, when no exception handler is used:

<?php
echo "I am flushed anyway";
exit; // flush() implicitly called with PHP+HHVM
?>

@joshuaspence
Copy link

I'm curious, why was this issue closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants