runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Fully support cancellation on all FileStream operations that take CancellationToken

Open madelson opened this issue 6 years ago • 6 comments

Being able to cancel IO operations that accept a CancellationToken is useful when working with pipes or other types of "files" where the read or write will often block. One example of this is monitoring for input on standard input: today there is no managed way to do this that can be aborted. Another example that has burned me several times is reading from/writing to the standard IO streams of other processes.

On Windows, cancellation could be performed via CancelIoEx if the file has been opened asynchronously, or via CancelSynchronousIo if the file has been opened asynchronously.

On Linux, it looks like we always do synchronous IO. From a bit of research, I get the sense that the typical approach for cancellating reads is to open a pipe and then precede each read call with select or poll on the pipe and the file. Then, writing to the pipe can unblock the call and be used as a trigger for cancellation logic. This pipe could be set up lazily and then cached with the stream to be disposed when the stream is disposed. Write/flush seem a bit hairier, but there are some suggestions here. Additionally, on Linux we sometimes go through SemaphoreSlim.WaitAsync() on the way, which is fully cancel-able.

Once we consistently support cancellation, then as a side benefit we can support ReadTimeout/WriteTimeout which today throw when set (CanTimeout returns false).

I also think that the current behavior violates the "principle of least surprise" a bit: there is no way to know that FileStream.ReadAsync(byte[], int, int, CancellationToken) silently does not support cancellation.

Obviously, each of these methods of supporting cancellation requires some additional overhead on each read/write/flush operation. However, since we can check CancellationToken.CanBeCanceled, we don't need to pay any of this overhead unless the caller explicitly requested it by passing us a cancel-able token.

madelson avatar Jan 10 '20 10:01 madelson

See also https://github.com/dotnet/runtime/issues/31390, especially my comment here: https://github.com/dotnet/runtime/issues/31390#issuecomment-559392741

poizan42 avatar Jan 11 '20 22:01 poizan42

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

Bump

poizan42 avatar Sep 04 '24 09:09 poizan42

Obviously, each of these methods of supporting cancellation requires some additional overhead on each read/write/flush operation. However, since we can check CancellationToken.CanBeCanceled, we don't need to pay any of this overhead unless the caller explicitly requested it by passing us a cancel-able token.

@madelson do you mind if I ask you what overhead exactly you are alluding to here? I've not heard of overhead caused by the use of CancellationToken before so your comment made me curious here. I'd like to understand if this overhead would also apply to my own code and whether I should then also be checking this CanBeCanceled property on my own services.

EDIT: Thanks @poizan42 for the explanation, that makes more sense to me now.

julealgon avatar Sep 04 '24 13:09 julealgon

Obviously, each of these methods of supporting cancellation requires some additional overhead on each read/write/flush operation. However, since we can check CancellationToken.CanBeCanceled, we don't need to pay any of this overhead unless the caller explicitly requested it by passing us a cancel-able token.

@madelson do you mind if I ask you what overhead exactly you are alluding to here? I've not heard of overhead caused by the use of CancellationToken before so your comment made me curious here. I'd like to understand if this overhead would also apply to my own code and whether I should then also be checking this CanBeCanceled property on my own services.

Is your code doing something with the CancellationToken besides checking whether its cancelled and passing it on to other methods? If not then there is no overhead - not in your code at least. This is only really relevant when you are implementing the low-level apis where cancellations needs to be translated into calls to the OS / platform.

You need to register a callback with CancellationToken.(Unsafe)Register or set up something that waits on CancellationToken.WaitHandle if you want to handle a cancellation asynchronously. No matter what you do this information needs to be stored somewhere. In the case of cancelling sync IO you also need to store a handle or reference to the thread that is blocked so it can be unblocked by CancelSynchronousIo()

poizan42 avatar Sep 04 '24 14:09 poizan42

@julealgon all I meant is that supporting cancellation invariably adds some overhead vs. just doing the plain IO without cancellation, whether it is just additional allocations for the registration or extra IO calls. I only mentioned this because I know the .NET team is (rightfully) averse to adding overhead just to support narrow use-cases, but I think that in this case any overhead can be made opt-in.

madelson avatar Sep 04 '24 23:09 madelson

My company has also run into this a number of times, working with devices on Linux. The main issue is that we'll have any kind of failure that hits this case and the only way to exit the application or resolve the issue is to hard kill the entire application process. We're debating whether we can make sure that this can never happen under any circumstances, or whether we need to write our own interop library to replace FileStream. If this were somehow recoverable, it'd be less of an issue.

I've looked at trying to fix it, but there are so many layers of abstraction in the runtime that I don't have much confidence in making the change without breaking a lot of other things. If the maintainers are interested in accepting a patch, I'll give it a shot.

wjohnstonsolventum avatar Nov 07 '25 15:11 wjohnstonsolventum