Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

"Operation now in progress" problem with a pipe

by mje (Curate)
on Nov 21, 2016 at 15:54 UTC ( #1176256=perlquestion: print w/replies, xml ) Need Help??
mje has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks.

I have some perl reading messages from a socket and then taking action on them. The server end of the socket expects to get a "heartbeat" msg every so often. The actions sometimes take too long and the heartbeat is not sent so the code was converted to fork a child something like this:

pipe($child_reader, $child_writer); binmode($child_reader, ":encoding(UTF-8)"); binmode($child_writer, ":encoding(UTF-8)"); $child_reader->autoflush(1); $child_writer->autoflush(1); # fork child my $pid = fork; if (!defined($pid)) { warn "Failed to fork child process - turning off --fork_wr +iter"; return; } if ($pid) { # parent close $child_reader or warn "Parent: failed to close pipe +reader - $!"; return; } else { # child close $child_writer or warn "Child: failed to close pipe w +riter - $!"; child_writer(); exit; }

and all the child_writer() does is:

my $select = IO::Select->new(); $select->add(fileno $child_reader); $last_heartbeat_sent = gmtime(0); my $buf = ''; PROCESS: while (1) { my @ready = $select->can_read(5); if (scalar(@ready)) { my $read = sysread($child_reader, $buf, 64*1024, length($b +uf)); if (!defined($read)) { warn "Failed read on pipe to parent - $!"; last PROCESS; } elsif ($read == 0) { # EOF warn "EOF on pipe to parent"; last PROCESS; } else { while ($buf =~ s/^(.*)\r\n//) { send_message($1); # send msg down socket to server } } } else { my $now = gmtime; if (($now - $last_heartbeat_sent) > 25) { send_message("<Heartbeat/>\r\n"); $last_heartbeat_sent = $now; } } }

The problem is that although the above fixed the heartbeat issue the parent was still trying to do too much so now the parent forks other processes to handle different events received and they are all effectively writing something down the same pipe to the child above.. Occasionally what happens is I get an error writing to the pipe from code like this:

# $msg is the message to send, it is guaranteed < PIPE_BUF and + ends in \r\n my $sigpipe = 0; local $SIG{PIPE} = sub { $sigpipe = 1; }; my $sent = print $child_writer $msg; if (!$sent && ($!{EPIPE} || $sigpipe)) { warn "Write to child_writer failed with EPIPE - child died + ($sigpipe)"); # restarts child writer } else { warn "Write to child_writer failed - $!"; }

and the the last warning sometimes outputs "Write to child_writer failed - Operation now in progress". I don't understand this since the write end of the pipe is blocking and so how can I get EINPROGRESS errror? Any ideas?

Replies are listed 'Best First'.
Re: "Operation now in progress" problem with a pipe
by mje (Curate) on Nov 21, 2016 at 20:42 UTC

    I think this is probably the fault of the "if" test in the last block of code which says if !sent && various and so we end up in the else even if bytes were written down the pipe successfully.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1176256]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2019-04-22 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (112 votes). Check out past polls.

    Notices?