Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: I got IPC::Open3 to work!

by siracusa (Friar)
on Jul 23, 2005 at 00:57 UTC ( [id://477404]=note: print w/replies, xml ) Need Help??


in reply to I got IPC::Open3 to work!

It doesn't look like you're handling partial reads or other exceptional conditions in your call to sysread(). Something like this might be a start.

use POSIX qw(EINTR EWOULDBLOCK EAGAIN); ... # Try to read 4096 bytes from $fh using sysread(). Icky. my $data; my $offset = 0; my $length = 4096; while($length > 0) # Handle partial reads { my $read = sysread($fh, $data, $length, $offset); unless(defined $read) { next if($! == EINTR || $! == EWOULDBLOCK || !$ == EAGAIN); # Handle other errors here ... } last if($read == 0); # EOF $offset += $read; $length -= $read; }

In practice, the details are somewhat reliant on the exact nature of the read(2) system call on your OS. The code above almost certainly does NOT handle all potential errors, but it's a start.

The big point is that you can't expect sysread() to actually read as much data as you ask it to read. At the very least, you have to handle partial reads and then either loop until you've read what you want (as shown above) or make some other sort of decision.

What you definitely don't want to do is treat something like an undef return caused by EINTR as a fatal error. That type of thing tends to crop up at the worst times (e.g., on a busy machine but never on your idle development box).

sysread() has similar annoyances associated with it. If it's at all possible to use the blocking versions (read() and write()) it will make your life a lot easier.

Replies are listed 'Best First'.
Re^2: I got IPC::Open3 to work!
by ikegami (Patriarch) on Jul 23, 2005 at 07:31 UTC
    It doesn't look like you're handling partial reads

    He does. Relevant code:

    while(my @ready = $select->can_read) { ... my $data; my $length = sysread $fh, $data, 4096; ... $processeddata .= $data; ... }

    All the data ends up in processeddata, no matter how much is read by a given call to sysread.

    It doesn't look like you're handling ... other exceptional conditions

    He does this too. Relevant code:

    if( ! defined $length || $length == 0 ) { $err .= "Error from child: $!\n" unless defined $length; $select->remove($fh); }

    The filehandle is removed from the list of handles monitored by select. I'm guessing can_read will return false when it no longer monitors any handles.

    Something like this might be a start.

    That's bad! You removed select, which is cruicial here.

      All the data ends up in processeddata, no matter how much is read by a given call to sysread.

      Oops, my bad. Actually, hm, I'm not sure what happens without the offset adjustments that I have in my code...

      It doesn't look like you're handling ... other exceptional conditions

      He does this too. Relevant code:

      if( ! defined $length || $length == 0 ) { $err .= "Error from child: $!\n" unless defined $length; $select->remove($fh); }

      That treats things like EINTR as "fatal" errors (stop listening on that $fh), which is probably not what he wants.

      You removed select, which is cruicial here.

      I was just commenting on the vagaries of sysread().

        Anything unexpected here is a fatal error. Unfortunately, I spent so much time getting this to work at all my boss isn't willing to devote any more time on the finer points. It'll end up biting me in the ass later, but I'm logging any error messages and autoforwarding these to my phone so I can fix (or at least be aware) them before my boss finds out.

        Harley J Pig

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://477404]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found