While writing a test on work I hit a bug in perl. Normally if recv is interrupted by a signal it returns undef and sets $! to EINTR. In my case I had failure because $! was set to zero. It took me some time to reproduce conditions in which it happens, but finally I've got it boiled down to the following:
use strict;
use warnings;
use IO::Socket::INET;
my $srv = IO::Socket::INET->new(
LocalAddr => '127.0.0.1',
LocalPort => 7777,
ReuseAddr => 1,
Listen => 1,
) or die $!;
my $sock = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 7777,
) or die $!;
my $cli = $srv->accept or die $!;
my $sock2 = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 7777,
) or die $!;
my $cli2 = $srv->accept or die $!;
local $SIG{ALRM} = sub { $sock->send( "hi\n", 0 ); };
alarm 2;
my $res;
use Data::Dumper;
$res = $sock2->recv( my $buf, 1024 ) or die Dumper [ $res, $!, 0 + $!,
+ ];
__END__
$VAR1 = [
undef,
'',
'0'
];
The trick was to establish two connections to the same peer, with only one connection I was getting correct result. Apparently this was fixed somewhere in 5.13, because if I run this with 5.14.2 it returns expected result, which is:
$VAR1 = [
undef,
'Interrupted system call',
'4'
];
That's the downside of using stable Debian -- you're hitting bugs that were already found and fixed years ago. I couldn't find it in RT though, so if somebody by any chance know what I'm talking about and can point me to the ticket or commit in git it would be much appreciated. I naturally fixed the problem in my module with:
my $ret = $self->{_socket}->recv( my $buffer, 131072 );
unless ( defined $ret ) {
- next if $! == EINTR;
+ next if $! == EINTR or $! == 0;
confess "Error reading reply from server: $!";
}
But now I have some doubts -- maybe
recv sets
$! to 0 for some other errors too.
Update: for the record, the commit that fixes the problem is d016601
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.