Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I know prior to 5.8 it wasn't "safe" to do much in a signal handler. How "safe" was/is it to call die()? Thanks

Replies are listed 'Best First'.
Re: How "safe" is die() in %SIG?
by Elian (Parson) on May 19, 2003 at 19:49 UTC
    Prior to 5.8, it was never safe to call die from within a real signal handler. ($SIG{__WARN__} and $SIG{__DIE__} aren't real signal handlers as such, though calling die from within them can cause some interesting things to happen as well) If you have safe signals enabled for 5.8 (and it may be the default these days, I really don't remember) then it will be safe.
      Thanks. I was referring to calling die() in a SIG{INT}... I tried it in 5.6, and as expected, sometimes it works as advertised, sometimes it causes a core dump, and sometimes it just "goes away". I guess it's really time to go to 5.8.
        Calling die() inside a $SIG{INT} handler is definitely asking for trouble. Perl can and will segfault, as you've found. die allocates memory, which is a no-no inside an interrupt handler, hence the problem. 5.8 takes care of this, though there are downsides to that related to alarm and blocking system calls.
Re: How "safe" is die() in %SIG?
by bart (Canon) on May 19, 2003 at 18:58 UTC
    You mean $SIG{__DIE__}? That's not actually a real signal handler. So die() is very safe.

    What isn't safe is asynchronous signal handlers, those that can be invoked wherever your script is at, even in the middle of executing another, non-reentrant action. die()is executed synchronously, so it's very safe indeed.