okio icon indicating copy to clipboard operation
okio copied to clipboard

Timeouts on FileInputStream-backed Sources

Open hturki opened this issue 5 years ago • 2 comments

Hello,

After reading the documentation available, I would expect this simple example to potentially time out:

Source source = Okio.source(new FileInputStream(file)); source.timeout().timeout(10, TimeUnits.SECONDS); source.readByte();

In this case, file is a unix named pipe (ie: created using "Runtime.getRuntime().exec("mkfifo " + pipe.toString()).waitFor();") However, when the pipe writer doesn't write anything to the pipe, the read hangs indefinitely (ie: the timeout is never triggered). Is this scenario not supported?

hturki avatar Jan 09 '20 22:01 hturki

We don’t yet implement timeouts on Files but it’s a useful idea. It might be relatively straightforward to do with FileInputStream.getChannel().close().

But I had difficulty reproducing your example because on my Mac new FileInputStream(file) blocks until the named pipe itself has an EOF, which I find odd.

swankjesse avatar Mar 20 '20 01:03 swankjesse

Upon further debugging I could get it working on macOS. But I wasn’t able to break out a blocking FileInputStream read by either closing the channel or interrupting the thread.

My suspicion is that blocking Java File IO doesn’t implement cancelation.

The fix for this will be to change Source and Sink to be backed by NIO channels instead of blocking ones. This is something we’ve occasionally considered but this particular bug is a compelling reason.

swankjesse avatar Mar 20 '20 02:03 swankjesse