Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task.WaitAll hanging with multiple awaitable tasks in ASP.NET

Below is a simplified version of the code I'm having trouble with. When I run this in a console application, it works as expected. All queries are run in parallel and Task.WaitAll() returns when they are all complete.

However, when this code runs in a web application, the request just hangs. When I attach a debugger and break all, it shows that execution is wait on Task.WaitAll(). And the first task has completed, but the others never finish.

I can't figure out why it hangs when running in ASP.NET, but works fine in a console application.

public Foo[] DoWork(int[] values) {     int count = values.Length;     Task[] tasks = new Task[count];      for (int i = 0; i < count; i++)     {         tasks[i] = GetFooAsync(values[i]);     }      try     {         Task.WaitAll(tasks);     }     catch (AggregateException)     {         // Handle exceptions     }      return ... }  public async Task<Foo> GetFooAsync(int value) {     Foo foo = null;      Func<Foo, Task> executeCommand = async (command) =>     {         foo = new Foo();          using (SqlDataReader reader = await command.ExecuteReaderAsync())         {             ReadFoo(reader, foo);         }     };      await QueryAsync(executeCommand, value);      return foo; }  public async Task QueryAsync(Func<SqlCommand, Task> executeCommand, int value) {     using (SqlConnection connection = new SqlConnection(...))     {         connection.Open();          using (SqlCommand command = connection.CreateCommand())         {             // Set up query...              await executeCommand(command);              // Log results...              return;         }     }            } 
like image 776
John Rutherford Avatar asked Oct 19 '12 19:10

John Rutherford


People also ask

Does Task WhenAny start the tasks?

As others have noted, Task. WhenAll only aggregates the tasks; it does not start them for you.

What is the difference between task WaitAll and task WhenAll?

The Task. WaitAll blocks the current thread until all other tasks have completed execution. The Task. WhenAll method is used to create a task that will complete if and only if all the other tasks have completed.

Can we use async await with thread?

An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method. The async and await keywords don't cause additional threads to be created.

Can you tell difference between task WhenAll and task WhenAny?

WhenAll returns control after all tasks are completed, while WhenAny returns control as soon as a single task is completed.


1 Answers

Rather than Task.WaitAll you need to use await Task.WhenAll.

In ASP.NET you have an actual synchronization context. This means that after all await calls you will be marshaled back to that context to execute the continuation (effectively serializing these continuations). In a console app there is no synchronization context, so all of the continuations are just sent to the thread pool. By using Task.WaitAll in the request's context you're blocking it, which is preventing it from being used to handle the continuations from all of the other tasks.

Also note that one of the primary benefits of async/await in an ASP app is to not block the thread pool thread that you're using to handle the request. If you use a Task.WaitAll you're defeating that purpose.

A side effect of making this change is that by moving from a blocking operation to an await operation exceptions will be propagated differently. Rather than throwing AggregateException it will throw one of the underlying exceptions.

like image 128
Servy Avatar answered Sep 21 '22 23:09

Servy