question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReadAsync accepts a CancellationToken but ignores it

See original GitHub issue

Which 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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kfarmer-msftcommented, Nov 28, 2018

Tests pass. I expect this will be in our next release.

1reaction
lostllamacommented, Nov 28, 2018

I’m working on accepting the PR

Thank you 😃 and thanks to @SimonGustavsson for creating the PR.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found