-
-
Notifications
You must be signed in to change notification settings - Fork 851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use innerException for Task.AsUniTask
#486
Conversation
@@ -28,7 +28,7 @@ public static UniTask<T> AsUniTask<T>(this Task<T> task, bool useCurrentSynchron | |||
p.TrySetCanceled(); | |||
break; | |||
case TaskStatus.Faulted: | |||
p.TrySetException(x.Exception); | |||
p.TrySetException(x.Exception.InnerException); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if there are multiple InnerExceptions, always unwrapping.
This is because the behavior is the same as await Task.WhenAll()
.
Is this a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, it is more natural to get multiple exceptions when WhenAll.
I ended up going for the same behavior as await Task.WhenAll
, but maybe I should break it and still provide AggregateException.
Sorry, but I would like to discuss this a bit.
var t1 = Task.Run(() => 100);
var t2 = Task.Run(async () => { await Task.Delay(1000); throw new Exception("E1"); });
var t3 = Task.Run(async () => { await Task.Delay(5000); throw new Exception("E2"); });
var t = Task.WhenAll(t1, t2, t3);
try
{
// await WhenAll wait all result(even if contains error)
await t;
}
catch (Exception ex)
{
// show first Exception(E1)
Console.WriteLine(ex);
}
Console.WriteLine("----");
try
{
// same as .Exception
t.Wait();
}
catch (Exception ex)
{
// show AggregateException(E1, E2)
Console.WriteLine(ex);
} when access .Exception, Task create AggrgateException from holded exceptions. https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TaskExceptionHolder.cs,252 when await Task and if the task faulted, throw its first exception, even if it contained more than one. -- This PR implementation (throw an InnerException for one and an AggregateException for more than one) is OK. |
#422 #428
The problem is that if Task.AsUniTask throws an exception, it becomes an AggregateException.
It seems that there is a historical reason why Task holds AggregateException internally.
We have fixed it as such, as supplementing InnerException is probably sufficient in most cases.