On Monday, September 12, 2016 at 5:40:35 AM UTC-7, Aristotle Pagaltzis 
wrote:
>
> * Abelard <abelard...@gmail.com <javascript:>> [2016-09-12 08:36]: 
> > The difference being that before we call the original die handler, 
> > we reassign SIG{__DIE__} back to it, which I guess re-enables the 
> > recursive call protection. 
>
> Ah, makes sense. But there’s no mechanism that restores your own die 
> handler, is there? It seems to me this this will only ever modify one 
> exception – if it happens to be the one you wanted, great, otherwise, 
> not so great. 
>

Yes, you're right. I was able to trigger that problem.

>
> The only solution I could think of is a bit crazy: store a scope guard 
> in @_ which restores your die handler on DESTROY. 
>
> A less crazy solution might be here: 
> https://github.com/moose/Moo/blob/master/t/demolish-throw.t 
> Looks like the die handler implements the recursion check manually. 
>
> Another option is to not use the `goto` and just call $old_die_handler 
> normally. That works reliably as long you *know* that $old_die_handler 
> will call `die` (which happens to be the case here) instead of trying 
> to abandon the exception using `goto`. 
>
> If it does try to abandon the exception, you must call it with `goto` 
> also, otherwise it will not succeed. But as long as it never tries to 
> `die`, that’s fine, so that works too. 
>

Calling $old_die_handler directly does seem to work great, thanks.

What's the issue if $old_die_handler uses goto instead of die? This is a 
worry about the inifinite loop again, or something else?
I was trying to identify the problem with this code but it seems to be 
working okay:

use strict;
use warnings;

sub _old_die2 {
  die "in old die2: $_[0]";
}

my $app = sub {
  # override global DIE handler
  $SIG{__DIE__} = sub { goto &_old_die2 };

  my $old_die_handler = $SIG{__DIE__};

  local $SIG{__DIE__} = sub {
    my $msg = shift;
    unshift @_, $msg . " lots of details about system state";

    die @_ unless $old_die_handler;

    $old_die_handler->(@_);
  };

  eval { die "i'm not dead yet" };
  die "goodbye world!";
  return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ];
};

I get:

The application raised the following error:

  in old die2: goodbye world! at /home/a/tmp/hello.psgi line 24.
 lots of details about system state at /home/a/tmp/hello.psgi line 5.

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"psgi-plack" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to psgi-plack+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to