Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Sockets, eval , and DIE

by Wiggins (Hermit)
on Apr 17, 2014 at 16:12 UTC ( #1082660=perlquestion: print w/replies, xml ) Need Help??
Wiggins has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for edge condition precedences. Syswrite to a closed socket(closed at remote end) results in Perl termination, with or without eval. Are there edge conditions that allow this?

This code (without the eval and test blocks) can be seen on the botton of page 209 of Advance Perl Programming, #1. The MSG module.

while (@$rq) { my $msg = $rq->[0]; my $bytes_to_write = length($msg) - $offset; my $bytes_written = 0; while ($bytes_to_write) { #write the prefixed length print "_send: syswrite to sock prefix length\n"; eval { $bytes_written = syswrite ($sock, $msg, $bytes_to_write, $offset); }; if ($@){ print "_send: syswrite to sock error <$@>\n"; } print "_send: syswrite to sock returned <$bytes_written>\n"; if (!defined($bytes_written)) { if (_err_will_block($!)) { # Should happen only in deferred mode. Record how # much we have already sent. $conn->{send_offset} = $offset; # Event handler should already be set, so we will # be called back eventually, and will resume sending return 1; ...
This is Perl 5.14.
The consistant result, running in foreground, when the server is shutdown, and a client send is attempted is:
checkRealTTY:: starting UID=<UID> rIP=<> Acct=<root> _send entered <Msg=HASH(0x29987c8)> flush=<1> _send: syswrite to sock prefix length root@kali:/usr/share/msg/bin#

It is always better to have seen your target for yourself, rather than depend upon someone else's description.

Replies are listed 'Best First'.
Re: Sockets, eval , and DIE
by mr_mischief (Monsignor) on Apr 17, 2014 at 16:56 UTC

    You're talking about signals here, not regular error conditions within a single program. Writing to a closed pipe gets you a SIGPIPE signal.

    If you want to survive a signal, you need a signal handler for that signal. In perldoc you're wanting the perlipc section. There's a page for perlipc or run perldoc perlipc locally.

      BINGO !!
      SIGPIPE="IGNORE" allowed the code to continue. Now to wrestle with "Don't test for what you aren't prepared to handle!"

      It is always better to have seen your target for yourself, rather than depend upon someone else's description.

        Alternatively, one can use "send" instead of "syswrite". Like this

        use Socket qw(MSG_NOSIGNAL); use Errno; my $out = send($sock, $data, MSG_NOSIGNAL); unless(defined $out) { if($!{EPIPE}) { warn("Got broken pipe\n"); } }
        In this case SIGPIPE is not provided, only EPIPE system error happens, which allows handling of all errors in single place.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1082660]
Approved by GotToBTru
Front-paged by GotToBTru
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2017-05-25 19:27 GMT
Find Nodes?
    Voting Booth?