Search code examples
javanio

What can cause ReadableByteChannel.close() to block?


I'm attempting to run an external process with a timeout and read all of the output it produces. This is proving to be a surprisingly Herculean task. My basic strategy is to start a process using ProcessBuilder and read it's output in the main thread. Another thread is farmed off that waits for the timeout to expire and (if needed) call close() on the ReadableByteChannel that I have passed it, the call destroy() on the Process that was also passed to it.

This appears to work just fine on a process that prints infinite output, even getting the expected AsynchronousCloseException. However, when testing against a program that just sleeps infinitely the thread blocks on close:

private void terminateProcess() {
    try {
        System.out.println("About to close readChannel.");
        readChannel.close();
        System.out.println("Closed the readChannel.");
    } catch (IOException e) {
        // Not relevant
    }
}

I never see the second print statement and my test hangs forever. The javadoc says close() can block if another close is in operation, but there are no other calls to close() in my code. What else can cause this?


Solution

  • The reason the test hangs forever is that ReadableByteChannel.close() will block if a ReadableByteChannel.read() is blocking it.

    From the documentation, ReadableByteChannel in blocking mode (all channels are by default) will block until at least one byte is read if there is a byte free in your ByteBuffer, so the read will be blocking.

    If you read the code for class java.nio.channels.Channels$ReadableByteChannelImpl (found in Channels.java), you will see that ReadableByteChannelImpl is commented as "Not really interruptible" - and apparently, they mean it.

    If you call Process.destroy(), that will terminate a Process that has produced no input and cause an AsynchronousCloseException to be thrown from your reading thread.