ReadAsync accepts a CancellationToken but ignores it
See original GitHub issueWhich service(blob, file, queue, table) does this issue concern?
Blob, probably others
Which version of the SDK was used?
9.3.1
Which platform are you using? (ex: .NET Core 2.1)
NET Core 2.1
What problem was encountered?
CancellationToken is ignored when reading from a blob read stream. The code below will happily read to the end. For my test file which is 42,397,696 bytes, I get the following output:
True
True
True
True
True
True
True
True
True
True
True
True
Done
I’ve had a nose through the code and can see that the problem comes down to the ReadAsync
, which takes a CancellationToken
, but doesn’t use it.
How can we reproduce the problem in the simplest way?
var cts = new CancellationTokenSource();
var buffer = new byte[4 * 1024 * 1024];
var readStream = await blobRef.OpenReadAsync(null, null, null, cts.Token);
cts.Cancel();
int bytesRead = 0;
do
{
bytesRead = await readStream.ReadAsync(buffer, 0, buffer.Length, cts.Token);
Console.WriteLine(cts.Token.IsCancellationRequested);
}
while (bytesRead > 0);
Console.WriteLine("Done");
Console.ReadLine();
Have you found a mitigation/solution?
I’m using the following extension method found here
public static Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
{
return task.IsCompleted
? task
: task.ContinueWith(
completedTask => completedTask.GetAwaiter().GetResult(),
cancellationToken,
TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
}
like so:
bytesRead = await readStream.ReadAsync(buffer, 0, buffer.Length, cts.Token).WithCancellation(cts.Token);
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
NetworkStream.ReadAsync with a cancellation token never ...
The doc says that all timeouts are ignored on the Socket class when an async method is used "Timeout: This option applies to...
Read more >How do I cancel non-cancelable async operations?
When the token has cancellation requested, the ReadAsync operation in flight may observe that request and cancel its processing before it ...
Read more >Task expressions - F# | Microsoft Learn
ReadAsync has multiple overloads, one of which accepts a cancellation token. If you do not use this overload, that specific asynchronous read ...
Read more >Cancellation, Part 1: Overview
The “90% case” just takes a CancellationToken parameter and passes it down. There's one notable exception to this rule: you shouldn't pass down ......
Read more >Cancellation task C# - CodeProject
FileStream.ReadAsync() returns a Task , supports the asynchronous reading of data and it can be cancelled. You can use it like this.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Tests pass. I expect this will be in our next release.
Thank you 😃 and thanks to @SimonGustavsson for creating the PR.