0

I've looked numerous posts on task exception handling and I'm not clear how to resolve this issue. My application crashes anytime there is an exception in one of my tasks. I am unable to catch the exception and my application is left in an inoperable state.

UPDATE: This only seems to happen when calling the ExecuteAsync method in the DataStax Cassandra C# driver. This leads me to believe it's an issue in the driver itself. When I create my own task and throw an exception it works fine.

Most use cases seem to await all asynchronous calls, but in my case I need to fire off a group of asynchronous commands and then use WhenAll to await them together (rather than awaiting each one individually).

This is based off of this post which shows how to batch up tasks to send to a Cassandra database: https://lostechies.com/ryansvihla/2014/08/28/cassandra-batch-loading-without-the-batch-keyword/

This is the same practice recommended by Microsoft when you want to perform multiple async requests without having to chain them: https://social.msdn.microsoft.com/Forums/en-US/6ab8c611-6b0c-4390-933c-351e56b62526/await-multiple?forum=async

My application entry point:

public void Go()
{
   dbTest().Wait();

My async method...

private async Task dbTest() {

List<Task> tasks = new List<Task>();
Task<RowSet> resultSetFuture = session.ExecuteAsync(bind); // spawn a db exception
Task<RowSet> resultSetFuture2 = session.ExecuteAsync(bind);
Task<RowSet> resultSetFuture3 = session.ExecuteAsync(bind);

tasks.Add(resultSetFuture);
tasks.Add(resultSetFuture2);
tasks.Add(resultSetFuture3);
try
{
   await Task.WhenAll(tasks.ToArray());                   
}                
catch (Exception ex)
{
   ...
}

All indications are that WhenAll should properly catch any exceptions from async methods, but it just locks up my application in this case.

KingOfHypocrites
  • 9,316
  • 9
  • 47
  • 69
  • " locks up" as in "deadlock"? Have you tried to see what all thread are doing when app gets into that state? Side note: most likely issue is in some of the other code as `WhenAll` works exactly as you described (wraps exceptions from all tasks) - assuming exception is thrown in asynchronous part of the task. – Alexei Levenkov Jun 07 '15 at 21:02
  • I am assuming it's a deadlock. I've stripped my test code to bare bones inside a console app. You are looking at all the code that belongs to me other than some non-essentials. I am calling into another library for the async call, which is the datastax csharp driver that connects with Cassandra. – KingOfHypocrites Jun 07 '15 at 21:08
  • 1
    If it is deadlock you should see where each thread stuck - in VS: Debug->Windows->Threads (may need to "Debug -> Attach to process" if it does not happen when you start under debugger). – Alexei Levenkov Jun 07 '15 at 21:10
  • If the app deadlocks, how do you know there's an exception? – i3arnon Jun 07 '15 at 21:12
  • 1
    I just implemented basically what you have there in a console app, where each task is defined like so: `tasks.Add(Task.Run() => { throw new Exception("Task1"); })`. It does not lock up. However, using await Task.WhenAll() only throws the first exception. If use Task.WhenAll().Wait(), an AggregateException is thrown. See here: http://stackoverflow.com/questions/7340309/throw-exception-inside-a-task-await-vs-wait – glenebob Jun 07 '15 at 21:15
  • I know there is an exception because if I await them individually it returns a database error. I am not sure if it is deadlocking or not. My application just sits in limbo. I updated my code sample to show my entry point into the method. I am trying the thread debug Window but it's not clear to me what would denote a deadlock. I'm still looking. – KingOfHypocrites Jun 07 '15 at 21:19
  • Looking like from Thread window it is just sitting on a worker thread and never returns to the main thread. – KingOfHypocrites Jun 07 '15 at 21:24
  • Try alternatives, like `Task.WaitAll()` and `Task.WhenAll().Wait()`, to try and pinpoint the issue. If there's a deadlock causing one or more of the tasks to not complete, then of course WhenAll() will never return OR throw an exception. – glenebob Jun 07 '15 at 21:25
  • WaitAll() and WhenAll().Wait() have the same effect. – KingOfHypocrites Jun 07 '15 at 21:29
  • @glenebob I think it may be a bug in the Cassandra c-sharp driver. When I create my own task and throw an exception it works fine as well. I'm not sure why the exception wouldn't bubble up to me. I can only assume maybe the deadlock is in the driver itself. – KingOfHypocrites Jun 07 '15 at 21:39
  • Before any exception can "bubble up", all the tasks have to complete (throw or return success). Once that happens, the await code will then re-throw the exception from the task that failed first, or just return success. – glenebob Jun 08 '15 at 01:37

0 Answers0