Closed NN--- closed 4 years ago
This happens: The Task
gets marked cancelled, but the CancellationToken
on the Task
says that no cancellation was requested. Why? Because the task has a CancellationToken
from the default source. Why? Because Task.Run
uses Unwrap
and Unwrap
does not copy the CancellationToken
. I'll fix that.
I tried to find out the reason but didn’t succeed.
Simple test shows that a Task should throw even without a CancellationToken with cancellation requested (Tried in LinqPad):
TaskCompletionSource<int> tcs1 = new TaskCompletionSource<int>();
Task<int> t1 = tcs1.Task;
tcs1.TrySetCanceled();
t1.Wait();
So, I think I'll make it throw when canceled even if the CancellationToken.CancellationToken
does not throw.
I'll see if Unwrap should or should not copy the CancellationToken. This could be two bugs instead of one.
Trying the code above in .NET 2.0 throws an AggregateException with TaskCanceledException with a CancellationToken with IsCancellationRequested = true. While the same code in LinqPad has IsCancellationRequested = false.
I can also confirm that Unwrap should copy the CancellationToken.
Edit: that is a total of 3 things to fix:
Ok, this is the problem…
If TrySetCanceled
does not set a CancellationToken
, this line hangs: https://github.com/theraot/Theraot/blob/master/Framework.Core/System/Threading/Tasks/Task.cs#L738
And then #120 comes back.
I haven't found any nice workaround.
--
Edit: To be clear, the changes that I will fix the original issue. The line Console.WriteLine("Must not get there");
won't be reached. It is just that TrySetCanceled
will always result in a TaskCanceledException
with a cancelled token, while that is not true in .NET framework.
Is there an updated NuGet ?
Just submitted. NuGet 3.2.0.
Thanks a lot !
Works well.
I guess related to #120