java - What can cause ReadableByteChannel.close() to block? -
i'm attempting run external process timeout , read of output produces. proving surprisingly herculean task. basic strategy start process using processbuilder , read it's output in main thread. thread farmed off waits timeout expire , (if needed) call close() on readablebytechannel have passed it, call destroy() on process passed it.
this appears work fine on process prints infinite output, getting expected asynchronouscloseexception. however, when testing against program sleeps infinitely thread blocks on close:
private void terminateprocess() { try { system.out.println("about close readchannel."); readchannel.close(); system.out.println("closed readchannel."); } catch (ioexception e) { // not relevant } }
i never see second print statement , test hangs forever. javadoc says close() can block if close in operation, there no other calls close() in code. else can cause this?
the reason test hangs forever readablebytechannel.close() block if readablebytechannel.read() blocking it.
from documentation, readablebytechannel in blocking mode (all channels default) block until @ least 1 byte read if there byte free in bytebuffer, read blocking.
if read code class java.nio.channels.channels$readablebytechannelimpl (found in channels.java), see readablebytechannelimpl commented "not interruptible" - , apparently, mean it.
if call process.destroy(), terminate process has produced no input , cause asynchronouscloseexception thrown reading thread.
Comments
Post a Comment