Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Detect whether a writeable filehandle has closed?

by Perlbotics (Archbishop)
on Jan 13, 2021 at 18:51 UTC ( [id://11126862]=note: print w/replies, xml ) Need Help??

in reply to Detect whether a writeable filehandle has closed?

Writing to a closed pipe should trigger a SIGPIPE but it requires to write something to it first. A child that died for some reason should also trigger a SIGCHLD – perhaps you could detect that and take it into account prior to sending more data? I would write a wrapper that replaces print with a-priori checks (i.e. SIGCHLD sets a flag asynchonously) and post-checks (SIGPIPE)... at least under Linux. Don't know if theses assumptions are valid under Windows.

Replies are listed 'Best First'.
Re^2: Detect whether a writeable filehandle has closed?
by Fletch (Bishop) on Jan 13, 2021 at 20:10 UTC

    Checking my copy of Stevens APUE my reading (and vague recollections of actual behavior) confirms this; you're not going to get SIGPIPE until you actually write to the fd the child's closed. The SIGCHLD should be the first sign you get.

    use IPC::Run qw( start pump ); use Log::Log4perl qw( :easy ); Log::Log4perl->easy_init($DEBUG); $SIG{$_} = eval qq{sub{INFO qq{Got $_}; 1}} for qw(CHLD PIPE); INFO q{Starting}; my $in=q{}; my $chld = start(["bash","-c","sleep 5"],q{<},\$in); ## This will get CLHD and sleep will exit early INFO q{Sleeping}; my $r=sleep(10); WARN( qq{$r, $!} ); ## This will trigger PIPE $in .= qq{FOO}; pump $chld; ## This won't be reached. INFO q{Done} __END__ 2021/01/13 15:07:42 Starting 2021/01/13 15:07:42 Sleeping 2021/01/13 15:07:47 Got CHLD 2021/01/13 15:07:47 5, Interrupted system call 2021/01/13 15:07:47 Got PIPE ack Broken pipe: write( 6, 'FOO' ) at ....

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      With the implication that only the last write failed before the SIGPIPE so caching, or being able to regenerate, the written data may provide jdporter the recovery information he needs.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re^2: Detect whether a writeable filehandle has closed?
by jdporter (Paladin) on Jan 13, 2021 at 18:59 UTC

    Thanks! Those are definitely things to try. (I'm already catching SIGPIPE, but as I said I'm getting the signal "late".)

    But you bring to mind the idea that maybe I need to turn off buffering of the output!

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 01:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found