Re: Stumped: returning a read pipe from a function



kj <socyl@xxxxxxxxxxxxxxxxx> wrote:
In <20070821121717.488$RO@xxxxxxxxxxxxxx> xhoster@xxxxxxxxx writes:

Just call eof($rdr) right off the bat.

That works great, reducing the function to:

use IPC::Open3 'open3';
use Symbol 'gensym';

sub foo {
my $err = gensym;
my $pid = open3( my ( $wtr, $rdr ), $err,
'/some/command', @_ );
close $wtr;
die join '', <$err> if eof $rdr;
return $rdr;
}


I think you previously said that /some/command can legitimately
produce no output under some non-error conditions. If so, that means you
have to do something more than just check eof $rdr before dying, you also
need to check $err or $?. Assuming $? doesn't tell you anything you need
to know and $err tells it all, then something like:

if (eof $rdr) {
return $rdr if eof $err; # no error message, means no error
waitpid $pid,0; # avoid zombies if die isn't really fatal
die <$err>;
};
return $rdr;


Thanks.

I'm still mulling over the zombies issue...

Since you die on errors, rather than logging and going on (well,
you could be invoking this from an eval {} and doing logging from
outside, so maybe the die doesn't really mean die...) I was kind of
hoping this was a single-use program and you could just forget about
zombies.

Maybe you could double-fork so the OS cleans up zombies automatically,
although it isn't obvious to me how to do that cleanly and simply with
Open3. Or you could set $SIG{CHLD} to "IGNORE", but that might screw up
other parts of your code if those parts also use fork, system, etc. and are
expecting $SIG{CHLD} to stay at its default.

${ *$rdr{ SCALAR } } = $pid; # Yeeeew!

Hmm. That makes me wonder, when you do an ordinary pipe open
(not IPC::Open? open), the corresponding close automatically waits on the
child. How does it know what pid to wait on? The pid must be stored
somewhere in the resulting file handle, but where? I tried finding it with
Devel::Peek, but couldn't. Considering my lack of experience with
Devel::Peek, I guess that that isn't surprising.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.



Relevant Pages