Skip to content
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

Merged
merged 4 commits into from
Sep 8, 2023
Merged

Use innerException for Task.AsUniTask #486

merged 4 commits into from
Sep 8, 2023

Conversation

hadashiA
Copy link
Contributor

@hadashiA hadashiA commented Sep 1, 2023

#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.

@@ -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);
Copy link
Contributor Author

@hadashiA hadashiA Sep 1, 2023

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?

Copy link
Contributor Author

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.

@neuecc
Copy link
Member

neuecc commented Sep 8, 2023

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.
https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/TaskAwaiter.cs,148

--

This PR implementation (throw an InnerException for one and an AggregateException for more than one) is OK.

@hadashiA hadashiA merged commit b071eea into master Sep 8, 2023
4 checks passed
@hadashiA hadashiA deleted the hadashiA/inner-ex branch September 8, 2023 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants