runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Cancellation Token is not passed to ReadAsync

Open jlarso11 opened this issue 3 years ago • 9 comments

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

jlarso11 avatar Apr 13 '23 20:04 jlarso11

You probably should edit your post with the Type, as it is missing. Or, even netter, directly link to the source code.

Tornhoof avatar Apr 13 '23 21:04 Tornhoof

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.

teo-tsirpanis avatar Apr 13 '23 21:04 teo-tsirpanis

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:

area-System.IO, untriaged, needs-author-action, needs-area-label

Milestone: -

ghost avatar Apr 14 '23 01:04 ghost

This issue has been marked needs-author-action and may be missing some important information.

ghost avatar Apr 14 '23 01:04 ghost

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.

stephentoub avatar Apr 14 '23 01:04 stephentoub

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.

jlarso11 avatar Apr 14 '23 14:04 jlarso11

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.

stephentoub avatar Apr 14 '23 14:04 stephentoub

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.

teo-tsirpanis avatar Apr 14 '23 14:04 teo-tsirpanis

Closing as answered.

stephentoub avatar Apr 16 '23 15:04 stephentoub