Task.WhenAny and Unobserved Exceptions

2019-01-18 10:05发布

Let's say I have three tasks, a, b, and c. All three are guaranteed to throw an exception at a random time between 1 and 5 seconds. I then write the following code:

await Task.WhenAny(a, b, c);

This will ultimately throw an exception from whichever task faults first. Since there's no try...catch here, this exception will bubble up to some other place in my code.

What happens when the remaining two tasks throw an exception? Aren't these unobserved exceptions, which will cause the entire process to be killed? Does that mean that the only way to use WhenAny is inside of a try...catch block, and then somehow observe the remaining two tasks before continuing on?

Follow-up: I'd like the answer to apply both to .NET 4.5 and .NET 4.0 with the Async Targeting Pack (though clearly using TaskEx.WhenAny in that case).

2条回答
趁早两清
2楼-- · 2019-01-18 10:58

What happens when the remaining two tasks throw an exception?

Those Tasks will complete in a faulted state.

Aren't these unobserved exceptions, which will cause the entire process to be killed?

Not anymore.

In .NET 4.0, the Task destructor would pass its unobserved exception to TaskScheduler.UnobservedTaskException, which would terminate the process if unhandled.

In .NET 4.5, this behavior was changed. Now, unobserved exceptions get passed to TaskScheduler.UnobservedTaskException, but then they are ignored if unhandled.

查看更多
乱世女痞
3楼-- · 2019-01-18 11:05

Yes, the remaining task exceptions are unobserved. Pre .NET 4.5 you are obliged to observe them (not sure how the situation is on .NET 4.5, but it changed).

I usually write myself a helper method for fire-and-forget tasks like these:

    public static void IgnoreUnobservedExceptions(this Task task)
    {
        if (task.IsCompleted)
        {
            if (task.IsFaulted)
            {
                var dummy = task.Exception;
            }
            return;
        }

        task.ContinueWith(t =>
            {
                var dummy = t.Exception;
            }, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
    }

You might want to include logging in production apps.

查看更多
登录 后发表回答