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

Greetings monks,
 
This is a followup on my earlier query: syswrite and closed sockets. Although the excellent guide by fellow monks solved many of the problems, however I hit upon one more issue and request your insights.
 
I have the following piece of code, which uses IO::Socket::INET to open a blocking socket against a messaging server and attempts to write a text:
use IO::Socket::INET; use Carp; my $socket = IO::Socket::INET->new( PeerAddr=> $host, PeerPort=> $port, Proto=> "tcp", Blocking => 1 ) ; for(1..100){ print $_ . ".."; sleep(2); eval{ $output = $socket->syswrite($string); }; croak "Could not syswrite: $@\n" if(!$output or $@); print "done\n"; }
 
While this code is running, I shutdown the message service. This causes the script to exit, after a couple of iteration. I was expecting to see the croak message, however the script just exits.
 
I tried to execute this script via Komodo IDE, and in the IDE output window I could see the croak message. Besides if I modified the error handling part to, lets say, write a message to a file and follow the process:
  • via a shell: no output and nor the file is being created
  • via Komodo: I see the croak message and the file is being created
 
My questions:
  • What is causing this behaviour ?
  • if syswrite does a exit, how can I handle it in a OO context ? (I know we can handle exits via WIFEXITED, but only if executed as a child process)

Replies are listed 'Best First'.
Re: syswrite, closed socket and error handling
by gcalexander (Novice) on Aug 15, 2006 at 00:56 UTC
    You have to trap or 'IGNORE' SIGPIPE. SIGPIPE is thrown when you write to a broken pipe. Having never used Komodo before, I'm guessing it installs its own signal handlers or something?? If you do
    $SIG{PIPE} = 'IGNORE';
    at the top of your script it won't die/exit silently. or you could do
    my $connected = 0; $SIG{PIPE} = sub { warn "Lost connection to server: $!"; $connected = +0; }; use IO::Socket::INET; use Carp; sub new_connection { return IO::Socket::INET->new( PeerAddr=> $host, PeerPort=> $port, Proto=> "tcp", Blocking => 1 ) ; } my $socket = new_connection() or die "$!"; my $output; for(1..100){ print $_ . ".."; sleep(2); if ($connected) { $output = $socket->syswrite($string); } else { $socket = new_connection (); or die "$!" $connected = 1; } print "done\n"; }
    By no means optimal at all, but just to give you an idea.