talexb has asked for the wisdom of the Perl Monks concerning the following question:

I thought I could use IO::Stream, but that doesn't seem to exist, even though CPAN returns 3500 results, including things like 'Audio::foo' and 'Bio::bar'.

In my system, there's a Java Application that opens a pipe to a port watched by an xinetd daemon, and this daemon fires up an application, then provides a pipe connection between that application and the Java application. Sometimes the Java Application crashes and isn't able to say "Gotta go -- bye!", so I'd like to be able to catch that condition and clean up after the pipe from the Java application is broken. My plan was to use IO::Stream, since I should be able to catch any exception from that input pipe.

Since this module doesn't exist, is there one that I could use instead?

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re: How to catch a broken input pipe
by Zaxo (Archbishop) on Jan 05, 2005 at 04:17 UTC

    Are you talking about named pipes or sockets? Your description doesn't match what standard pipe is able to do. An ordinary pipe requires some ancestral process to set the thing up.

    Normally, a fifo blocks on open until the other end is opened as well. An alarm timeout or a non-blocking sysopen polling in a sleepy loop can be used to detect the hang.

    Similarly, if you are talking about sockets, a timeout can be used to detect a lack of listeners.

    After Compline,
    Zaxo

Re: How to catch a broken input pipe
by Errto (Vicar) on Jan 05, 2005 at 04:38 UTC
    I assume you mean here that the application is written in Perl, and that the xinetd daemon opens it through a pipe and begins writing data to the applications STDIN. In that case you can use the simple <> operator on the Perl side, and as soon as the client connection is broken <> will return undef. More generally, <$fh> will return undef as soon as $fh is closed regardless of how it was opened (by creating a pipe, by accepting a client socket connection, etc).
Re: How to catch a broken input pipe (can't)
by tye (Sage) on Jan 05, 2005 at 06:47 UTC

    If the writer on the other end of a "pipe" that you are reading from goes away suddenly, no matter how dire the reasons, you are likely to only be told "end of file", not any kind of error condition nor exception.

    You might be able to get a socket to tell you that the other side went away without properly closing the socket, but I actually think the OS will properly close the socket in such a way that you will be powerless to even detect that.

    - tye