Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Detect whether a writeable filehandle has closed?

by shmem (Chancellor)
on Jan 14, 2021 at 14:11 UTC ( #11126909=note: print w/replies, xml ) Need Help??

in reply to Detect whether a writeable filehandle has closed?

With unbuffered I/O, print returns undef when writing to a closed filehandle; for pipes, $! is then set to "Broken pipe". You don't get SIGPIPE after the child closed the pipe, but after an unsuccsessful write to that filehandle. I tried to use select (and IO::Select with $io->can_write()) to detect whether I can write to a filehandle before writing, to no avail. Both always report the filehandle to be apt for writing, even after receiving SIGPIPE. Only after closing the filehandle for writing in the parent (e.g. in the SIGPIPE handler) I get the expected result.

On linux, that is (debian). So, no way to check whether the end point of the pipe is still connected before writing to it.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: Detect whether a writeable filehandle has closed?
by jdporter (Canon) on Jan 14, 2021 at 14:48 UTC

    Thank you for that. I think I'd be ok if I got the SIGPIPE instantaneously, but as I said it appears that the signal has to "make its way through the system" before my handler code gets control, by which time I may have written any number of additional lines to the child. :-(

      As said, you get the SIPGPIPE immediately after the first unsuccessful write, so you may want to keep your amount of lines short for each write.

      If you have control of the child you could make it send a HUP or USR1 to its parent after closing the file handle.

      Update: Oh I see you haven't, a compiled C program... hm. A wrapper, maybe?

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2021-02-25 05:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found