0

I'm working on a small WPF desktop utility and am using the async/await methodology to allow things to process in parallel.

However, I keep running into issues where an awaited async Task simply never returns. No exceptions are thrown, and if I pause the app in the debugger, the call stack says it is running "External code" called by the line where the async task is called. (Specifically, it hangs at: WindowsBase.dll!System.Windows.Threading.DispatcherSynchronizationContext.Wait(System.IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) on my current example of this.) I've seen this several times and worked around it, but each time I have to add debugging code to figure out what line things stopped executing on. (The same code might execute fine for several iterations, and then hang) The same issue has happened both on awaiting my own async methods and on awaiting framework async methods E.G. Stream.CopyToAsync and Stream.ReadAsync)

Is there a way to look into executing Tasks in Visual Studio 2017? I tried opening the "Tasks" window, but I've never gotten anything but "No tasks to display" -- possibly I'm not using that window correctly?

FWIW, I'm doing a lot (hundreds) of concurrent background operations, but none overlap. Mostly web service calls, file system reads and MD5 checksum computations. Is the async/await limited in what it can do concurrently without freezing up? Or a maximum nesting of awaits?

6
  • There's nothing in the Output window? Commented May 31, 2017 at 18:48
  • Are you on Windows 7? The Tasks window isn't supported for it. Maybe take a look at the Threads window, this is less useful as async tasks don't necessarily run on different threads but you may get some useful information out of it. Commented May 31, 2017 at 19:52
  • 1
    This looks an awful lot like a deadlock due to the synchronization context. Do you wait synchronously on a task somewhere? (using task.Result or task.Wait()) Commented Jun 1, 2017 at 6:04
  • @SvdSinner, please also disable "Use Managed Compatibility Mode" under TOOLS->Options->Debugging->General. social.msdn.microsoft.com/Forums/vstudio/en-US/…. Commented Jun 1, 2017 at 6:07
  • @dukedukes sadly, my work machine is stuck on Windows 7, so that explains that issue. Commented Jun 1, 2017 at 14:04

1 Answer 1

8

This looks like a classic case of deadlock due to the synchronization context. This happens when you wait synchronously on a task returned by a method that internally calls await. For instance:

public void Deadlock()
{
    DoSomething.Wait();
}

public Task DoSomething()
{
    // Some stuff
    await DoSomethingElse();
    // More stuff
}

When inside of a synchronization context, the continuation of await is posted to that very same synchronization context. But the thread of the synchronization context is waiting on the DoSomething().Wait(), and therefore not available. We have a deadlock:

  • The continuation is waiting for the thread of the synchronization context to be available
  • The thread of the synchronization context is waiting for the task to be completed, which will happen when the continuation is executed

There are two possible fix:

  • Stop waiting synchronously on tasks
  • Use .ConfigureAwait(false) when awaiting, so that the continuation isn't posted to the synchronization context
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.