Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an exception in an async Parallel.ForEach crash the application? [duplicate]

Why does the following crash if run inside a console application instead of throwing an AggregateException and being caught by the outer try/catch?

I've simplified the use case for the await for brevity, but in the relevant code I am indeed trying to execute an awaitable Task of importance.

var list = new List<string>() {"Error"};

        try
        {
            Parallel.ForEach(list, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 8
            }, async listEntry =>
            {
                await Task.Delay(5000);
                throw new Exception("Exception");
            });
        }
        catch (Exception ex)
        {
            //never hits, the application crashes
        }

        Console.ReadLine();

I note that the following does not cause the application to fail, and an exception is indeed caught, but I do not understand what's really going on as to what is fundamentally different about the two contexts:

var list = new List<string>() {"Error"};

        try
        {
            Parallel.ForEach(list, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 8
            }, listEntry =>
            {

                throw new Exception("Exception");
            });
        }
        catch (Exception ex)
        {
            //exception is caught, application continues
        }

        Console.ReadLine();
like image 535
Grant H. Avatar asked Oct 19 '25 12:10

Grant H.


1 Answers

As already mentioned in comments, you shouldn't mix async and Parallel.ForEach, they don't work together.

What you're observing is one of the consequences of that: the lambda is compiled as an async void method and when an async void method throws, the exception is rethrown on its SynchronizationContext, which will normally crash the application.

like image 115
svick Avatar answered Oct 21 '25 02:10

svick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!