Search code examples
c#taskaggregateexception

System.AggregateException on Task.WaitAll


I have the following code which is firing off a number of async Task.

        List<TimeoutException> TimeoutExceptions = new List<TimeoutException>();
        List<TaskCanceledException> TaskCanceledExceptions = new List<TaskCanceledException>();
        List<Exception> Exceptions = new List<Exception>();
        List<AggregateException> AggregateExceptions = new List<AggregateException>();

        List<Models.Channel.IChannel> channels = new List<Models.Channel.IChannel>();            
        channels.Add(new Models.Channel.DummyChannelName());            

        var tasks = new List<Task>();
        foreach (Models.Channel.IChannel channel in channels)
        {
            try
            {
                var cts = new CancellationTokenSource();                    
                cts.CancelAfter(channel.TimeOut);

                tasks.Add(Task.Run(() =>
                { 
                    channel.Data = channel.RequestOffers(new Models.Request.AvailabilityRequest()).Result;

                    if (cts.Token.IsCancellationRequested)                        
                        cts.Token.ThrowIfCancellationRequested();

                }, cts.Token));
            }
            catch (TimeoutException t)
            {
                TimeoutExceptions.Add(t);
            }
            catch (TaskCanceledException tc)
            {
                TaskCanceledExceptions.Add(tc);
            }
            catch (AggregateException ae)
            {
                AggregateExceptions.Add(ae);
            }
            catch(Exception ex)
            {
                Exceptions.Add(ex);
            }

        }

        Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(5));

The problem I have is that if a task is cancelled because of a timeout I'm getting the following exception

<ExceptionMessage>One or more errors occurred.</ExceptionMessage>
<ExceptionType>System.AggregateException</ExceptionType>

Is it just a simple case that I need a Try Catch around Task.WaitAll, or should my code be structured differently.


Solution

  • If an exception occurs inside a task the exception is raised on the calling thread when you Wait for a task. If a task is sucessfully created all exceptions that occur inside the task are wrapped in an AggregateException and thrown on the wait.

    For you example this means you can remove the try/catch block within your loop and use it to wrap the Task.WaitAll(...) after the loop.

    var tasks = new List<Task>();
    foreach (Models.Channel.IChannel channel in channels)
    {
        Task myTask = Task.Run(...); // create your task here
        tasks.Add(myTask);
    }
    
    try
    {
        Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(5));
    }
    catch
    {
        // Insert Exception handling logic
    }