Hung out on a broken pipe
843790May 20 2009 — edited May 22 2009One server in our system uses named pipes in Linux to facilitate communications between Java and C processes running on it.
When the pipe, that is read by the Java side, is broken (the C-side process that feeds it is stopped), Java will block trying to instantiate a new FileReader, FileInputStream and RandomAccessFile (in the "r" mode) on that broken pipe. Once blocked, that's it.
My first thought was to create a FileChannel since they are interruptible but you can only get a FileChannel from a FileInputStream or RandomAccessFile and creating those is where the problem is.
In other words, Java seems to be blocking/hanging on the open making it so I can't even get to a read that can be interrupted.
Q1) Any ideas how to keep from blocking on the broken pipe? I can open a java.io.File on the pipe and check exists(), isFile(), length(), canRead() and those all work. length() returns 0, but I'm fairly sure it will do that even if the pipe has content. So those methods won't help.
Q2) If I run the reader in a separate Thread and use my own timeout timer, how do I kill the blocked Thread without resorting to deprecated methods?
Thanks for any thoughts.
Kevin