Closed kmandelbaum closed 4 months ago
Unfortunately, this is a fundamental limitation of blocking IO, which is necessary for ordinary files. There is nothing Tokio can do about it. We have methods for reading from a fifo that work in a non-blocking manner in tokio::net::unix::pipe
.
One thing you can do is to shut down your runtime with shutdown_timeout
. That will let you exit even if there are hanging blocking IO operations. But it is of course best to avoid them.
@Darksonn thanks a million!
Yep, I've done some more research, learned a bit more about epoll
. tokio::net::unix::pipe
does indeed work. I think this should be closed, if it's a well-known limitation. I wonder if tokio::fs
could detect if the file is a pipe, and choose the "right" implementation, though it's arguable what is right in that case.
Version
Platform
Description
It looks like file reads are not fully cancelled (i.e. block the runtime / hold on to resources) by
timeout
orselect!
until theread
syscall returns.I tried this code:
I expected to see this happen: the program terminates immediately after printing "Reading done".
Instead, this happened: The program prints "Reading...", and then, after one second, "Reading done", then hangs indefinitely until I send something to stdin.
Tried this with
stdin
and files created bymkfifo
, also tried cancellation usingtimeout
andselect!
betweenread
andtokio::time::sleep
, also tried differentread*
methods with the same result - in all those cases the program hangs in the end until there's input to be read.When doing the same thing with reading from a TCP socket, the program behaves as expected - terminates immediately after printing "Reading done". When doing an equivalent thing in
async-std
, the program behaves as expected.The relevant bits of
strace
look like thisI wonder if that's an implementation quirk, and whether this kind of thing matters at all. For my immediate use case it doesn't because I can do
std::process::exit
and be done with it. That's a workaround though, and there could be a concern with resources leaking because of this. If I runopen
(for a FIFO) + timeout code in the loop, it seems to be leaking FDs (based on looking into/proc/$pid
).So this one
accompanied by
mkfifo in; cat > in
in another terminal also quickly reaches "Reading done", hangs, and shows many FDs open in/proc
.