Skip to content

Commit

Permalink
Check for a system call interruption and retry
Browse files Browse the repository at this point in the history
A retry is only attempted if the syscall failure was an EINTR and a
timeout had been specified.
  • Loading branch information
gmunoz committed Sep 14, 2016
1 parent 425ddf0 commit 34e32c1
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions lib/Net/Cmd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,19 @@ sub getline {
my $rin = "";
vec($rin, $fd, 1) = 1;

my $timeout = $cmd->timeout || undef;
my $initial = time;
my $pending = $timeout;

my $buf;

until (scalar(@{${*$cmd}{'net_cmd_lines'}})) {
my $timeout = $cmd->timeout || undef;
my $rout;

my $select_ret = select($rout = $rin, undef, undef, $timeout);
if ($select_ret > 0) {
unless (sysread($cmd, $buf = "", 1024)) {
my $select_ret = select($rout = $rin, undef, undef, $pending);
if (defined $select_ret and $select_ret > 0) {
my $r = sysread($cmd, $buf = "", 1024);
if (! defined($r) ) {
my $err = $!;
$cmd->close;
$cmd->_set_status_closed($err);
Expand All @@ -361,6 +365,20 @@ sub getline {
push(@{${*$cmd}{'net_cmd_lines'}}, map {"$_\n"} @buf);

}
elsif (defined $select_ret && $select_ret == -1) {
if ( $! == EINTR ) {
if ( defined($timeout) ) {
redo if ($pending = $timeout - ( time - $initial ) ) > 0;
$cmd->_set_status_timeout;
return;
}
redo;
}
my $err = $!;
$cmd->close;
$cmd->_set_status_closed($err);
return;
}
else {
$cmd->_set_status_timeout;
return;
Expand Down

0 comments on commit 34e32c1

Please sign in to comment.