Cancellation Token is not passed to ReadAsync
Description
We use public virtual ValueTask<int> ReadAsync(Memory<char> buffer, CancellationToken cancellationToken = default) and pass in a cancellationToken. We then cancel the token but it won't exit out of the Read. We believe that this is because it then calls public virtual Task<int> ReadAsync(char[] buffer, int index, int count) and the token is not passed in. That ReadAsync method then calls internal virtual ValueTask<int> ReadAsyncInternal(Memory<char> buffer, CancellationToken cancellationToken) with cancellationToken=default.
Shouldn't the token be passed all the way down to that ReadAsyncInternal method?
Reproduction Steps
Call ReadAsync with a cancellationToken, cancel the token,
Expected behavior
The readAsync call will return immediate
Actual behavior
The read async won't return until the buffer is full.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
https://github.com/dotnet/runtime/blob/8a46f0777ef975bb3d39cfb0b477c8e5c2d02b9a/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs
You probably should edit your post with the Type, as it is missing. Or, even netter, directly link to the source code.
If you are creating your own descendant of TextReader, you have to override ReadAsync(Memory<char>, CancellationToken) to take advantage of its features (zero-copy and cancellability). Judging from your description, you most likely haven't.
Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.
Issue Details
Description
We use public virtual ValueTask<int> ReadAsync(Memory<char> buffer, CancellationToken cancellationToken = default) and pass in a cancellationToken. We then cancel the token but it won't exit out of the Read. We believe that this is because it then calls public virtual Task<int> ReadAsync(char[] buffer, int index, int count) and the token is not passed in. That ReadAsync method then calls internal virtual ValueTask<int> ReadAsyncInternal(Memory<char> buffer, CancellationToken cancellationToken) with cancellationToken=default.
Shouldn't the token be passed all the way down to that ReadAsyncInternal method?
Reproduction Steps
Call ReadAsync with a cancellationToken, cancel the token,
Expected behavior
The readAsync call will return immediate
Actual behavior
The read async won't return until the buffer is full.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
https://github.com/dotnet/runtime/blob/8a46f0777ef975bb3d39cfb0b477c8e5c2d02b9a/src/libraries/System.Private.CoreLib/src/System/IO/TextReader.cs
| Author: | jlarso11 |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
This issue has been marked needs-author-action and may be missing some important information.
The ReadAsync overload that takes a Memory<char> was added many releases after the ReadAsync(byte[], ...) was added. As such, in order to ensure that any override of ReadAsync(byte[], ...) is used, the base implementation of ReadAsync(Memory<char>, ...) delegates to it, and the ReadAsync(byte[], ...) doesn't accept a CancellationToken. If you look at all of the relevant TextWriter-derived types in the core libraries, like StreamWriter, they all override this method and appropriately used the token. As was noted, any implementations you have should do so as well.
We are using Console.In which looks like it does not have those methods overridden. We probably should derive our own type for that instead.
We are using Console.In which looks like it does not have those methods overridden
The default readers/writers on Console are synchronized, locking around every operation, and thus none of the operations actually runs asynchronously: for better or worse, they all just synchronously delegate to the synchronous methods on the underlying reader/writer.
SyncTextReader which Console.In uses does not have the Memory async methods overriden, but even if it did it would still not be cancellable; the existing async methods are implemented synchronously.
Closing as answered.