Repository navigation
Some cleanup work around cancellation tokens - #2443
Conversation
austindrenski
left a comment
There was a problem hiding this comment.
Just one question, otherwise looks good.
|
Hello, I added some sample code related to this work item in #2201 |
8225cb9 to
5512d8e
Compare
|
@YohDeadfall I've (finally) updated this PR, can you please give it a quick review so we can merge? |
|
@worldleaderpretend I'll take a look at the wait issue you raised separately, as we may want to patch 4.0.x. |
|
@worldleaderpretend can you please open a new issue with your repro? Checking the cancellation token in the method start will take care of it, but it does actually leave a race condition if cancellation is triggered slightly after that. |
|
Sure thing, here ya go #2506. Thanks for the fast response! |
There was a problem hiding this comment.
There is a tiny semantical difference here though.
Task.FromCanceled will create a Task.Status == Canceled with TaskCanceledException.
While throwing from within an ordinary async method would transition the task to Faulted with an OperationCanceledException (which is the base class for TaskCanceledException) because the task wasn't started with the passed in token.
There was a problem hiding this comment.
I may be misunderstanding, but consider the following:
[Test]
public void ThrowInAsyncAfterYield()
{
var t = Foo(new CancellationToken(canceled: true));
Assert.That(async () => await t, Throws.Exception.TypeOf<TaskCanceledException>());
Assert.That(t.Status, Is.EqualTo(TaskStatus.Canceled));
static async Task Foo(CancellationToken token)
{
await Task.Yield();
token.ThrowIfCancellationRequested();
}
}
[Test]
public void ReturnTaskCanceledInNonAsync()
{
var t = Foo(new CancellationToken(canceled: true));
Assert.That(async () => await t, Throws.Exception.TypeOf<TaskCanceledException>());
Assert.That(t.Status, Is.EqualTo(TaskStatus.Canceled));
static Task Foo(CancellationToken token)
=> token.IsCancellationRequested
? Task.FromCanceled(token)
: Task.CompletedTask;
}ThrowInAsyncAfterYield's task is ultimately Canceled, not Faulted.
You may also want to look at dotnet/corefx#38453 and the conversation on it.
There was a problem hiding this comment.
No sorry, have been meaning to reply. But it's interesting it works this way. Does that TaskCanceled exception also include that cancellationtoken from the arguments? In that case I'm really curious what magic the compiler does there.
There was a problem hiding this comment.
You're asking where (or whether) the token is stored on cancelled tasks, right? It could internally be a union with the exception that needs to be recorded on faulted tasks, for example...
Otherwise token.ThrowIfCancellationRequested() simply goes throw new OperationCanceledException(token), which gets caught and results in the task being Cancelled, as far as I understand it.
759096f to
e4c0005
Compare
|
@NinoFloris @YohDeadfall I've addressed your comments, can you please take another look? It would be good to merge this. |
e4c0005 to
90e580c
Compare
90e580c to
95b311e
Compare
* Make sure async methods returned cancelled tasks rather than throw
exceptions when passed a cancelled token.
* Added cancellation token parameter to
NpgsqlDataReader.Get{Stream,TextReader}Async(). This is a binary
breaking change.
* Added "default" to some cancellation token parameters which were
missing it.
Closes npgsql#2417
95b311e to
817a536
Compare
Closes #2417