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

Hi authorities, I am in following situation on my linux box, perl 5.8.3:
Parent process has forked child(s) and child sometimes sends some short strings to parent via IO::Socket::UNIX instance. When parent is sigkilled to mimize some weird situations, child immediatelly and quietly dies when attempting to print something into socket. I cannot test the socket, if the opposite site is sane or not, respectively, if write operation will be successive. Is there some way how to recognize that write operation will be fatal or successive?
pajout, the disciple :)

Replies are listed 'Best First'.
Re: How to detect dead socket
by gaal (Parson) on Feb 10, 2005 at 18:59 UTC
    Try installing a handler for SIGPIPE in the child. You do make the call, but don't die if it failed.
    our $PARENT_ALIVE = 1; $SIG{PIPE} = sub { $PARENT_ALIVE = 0 }; [...] $unix_sock->print($message) if $PARENT_ALIVE; if (! $PARENT_ALIVE) { print "note: parent died; will not attempt future messages.\n"; }

    It's been a while since I actually did this kind of thing, though, and this is untested!

    (You can also subclass IO::Socket::UNIX and override print to wrap this safety check for you, if you want.)

Re: How to detect dead socket
by mr_mischief (Monsignor) on Feb 10, 2005 at 19:04 UTC
    What you get when you write to a closed socket is the signal SIGPIPE. Like many signals, the default action for SIGPIPE is to give up and die. If you want your program not to die when it gets SIGPIPE, you can set up a signal handler for it that does something else. This, BTW, is not Perl specific. The same issue arises on a Unix-type OS under any language which doesn't implicitly ignore or automatically deal with signals.


    Christopher E. Stith