Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task.WhenAny and Unobserved Exceptions

Tags:

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

like image 685
David Pfeffer Avatar asked Oct 01 '12 17:10

David Pfeffer


People also ask

What is unobserved task exception?

An “unobserved” exception is one that's stored into the task but then never looked at in any way by the consuming code. There are many ways of observing the exception, including Wait()'ing on the Task, accessing a Task<TResult>'s Result, looking at the Task's Exception property, and so on.

How do you handle exceptions in tasks?

Exceptions are propagated when you use one of the static or instance Task. Wait methods, and you handle them by enclosing the call in a try / catch statement. If a task is the parent of attached child tasks, or if you are waiting on multiple tasks, multiple exceptions could be thrown.

What is task WhenAny?

WhenAny(Task, Task) Creates a task that will complete when either of the supplied tasks have completed. WhenAny<TResult>(Task<TResult>[]) Creates a task that will complete when any of the supplied tasks have completed.


1 Answers

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.

like image 143
Stephen Cleary Avatar answered Jan 01 '23 20:01

Stephen Cleary