in reply to Setting signal handlers considered unsafe?

What are you actually trying to do here? What did you expect to see?

Do you want to set a handler in the parent? You don't need to do anything fancy. Just set the handler in the parent. You can do it from a sub if you want. Don't do it in a while 1 loop, that's just asking for trouble. You don't need to local it, because it's a forked process at that point, it's totally separate from the child.

Once you go into mult-process and multi-thread, you really have to watch out. Strange error messages that are different every time you run are pretty normal if you don't have a good understanding of the non-sequentiality of these things.

--Pileofrogs

  • Comment on Re: Setting signal handlers considered unsafe?

Replies are listed 'Best First'.
Re^2: Setting signal handlers considered unsafe?
by gnosek (Sexton) on Nov 05, 2008 at 20:39 UTC

    Hi,

    First of all, thanks everybody for your numerous replies :)

    Second, I guess I should have written in my original post. I know a tight loop of setting a local signal handler is silly, just like calling kill in another tight loop. I don't do that in my code, really. It's just the simplest way of reproducing the issue. It happens, say, once a week in the real world and once a second in the demo code.

    Third, I do have a pretty good grasp of multiprocessing issues, including atomicity of signal handlers and (simplifying a bit) non-sharing of address space between different processes. I don't expect to learn why local $SIG{FOO} = \&bar; is silly, I expect to learn why it isn't atomic and what I can do about it.

    I have tried sigprocmask together with the totally undocumented POSIX::SigSet (does it have something like ->add?) but I can still easily "crash" perl with this code. Avoiding local and setting/resetting the handler "by hand" seemed to decrease the frequency in the real world but the race still exists.

    Also, strace shows that perl internally does mostly the same thing (block signal, call sigaction, unblock) when setting the handler so I don't think there's anything more I can do, other than cry for help in the Monastery ;)

      ... totally undocumented POSIX::SigSet ... Try POSIX
        D'oh! I've read through that page numerous times (and have learnt of POSIX::SigSet from there) without ever seeing it. My apologies.