One user scenario to rule them all

Sergey Tepliakov


The async series

Almost every non-trivial behavior of the async methods in C# can be explained based on one user scenario: migration of the existing synchronous code to asynchronous should be as simple as possible. You should be able to add async keyword before a method’s return type, add Async suffix to its name, add await keyword here and there in the method body to get a fully functional asynchronous method.


This “simple” scenario drastically affects the behavior of asynchronous methods in many different ways: from scheduling task’s continuations to exception handling. The scenario sounds plausible and important but it made simplicity behind the async methods very deceptive.

Synchronization context

UI development is one of the areas where the scenario mentioned above was especially important. Long-running operations in the UI thread make applications unresponsive and asynchronous programming was always considered a good fit there.

The code looks very simple, but now we have a problem. Most UI frameworks have the restrictions that only a dedicate thread can change the UI elements. This means that line 3 would fail if a task’s continuation is scheduled on a thread pool’s thread. Luckily this issue was relatively old and starting from .NET Framework 2.0 the notion of Synchronization Contexts was introduced.

Each UI framework provides special utilities for marshaling work into a dedicated UI thread (or threads). Windows Forms relies on Control.Invoke, WPF – on Dispatcher.Invoke and yet another framework may rely on something else. The concept is similar in all the cases, but the underlying details are different. Synchronization context abstracts away the differences and provides an API for running the code in a “special” context leaving the details to the derived types like WindowsFormsSynchronizationContext, DispatcherSynchronizationContext etc.

To solve the thread-affinity problem the C# language authors decided to capture a current synchronization context at the beginning of the async methods and schedule all the continuations into the captured context. Now every block between awaitstatements is executed in the UI thread that made the main scenario possible. But the solution introduced a whole bunch of other challenges.


Let’s review a small and relatively simple piece of code. Are there any issues here?

The code will cause a deadlock. The UI thread starts an async operation and synchronously waits for the result. But the async method can’t be finished because the second line of GetStockPricesForAsync is should run in the UI thread causing the deadlock.

You may argue, that the issue is relatively easy to spot, and I’ll agree with you. Any calls to Task.Result or Task.Wait should be banned from the UI code, but the problem still possible if the component that the UI code relies on, synchronously waits for an async operation’s result:

This code causes the deadlock as well. And now two “well-known” best practices for asynchronous programming in C# should make much more sense:

  • Do not block an async code via Task.Wait() or Task.Result and
  • Use ConfigureAwait(false) in a library code.

The first advice should be clear already and we should have another section to explain the other one.

Configure “awaits”

There are two reasons why the last example caused the deadlock: blocking call to Task.Wait() in GetStockPricesForAsync and the synchronization context implicitly used in the continuation inside InitializeIfNeededAsync. Even though C# authors discouraged use of blocking calls of async methods it was clear that there are plenty of cases when this may happen. To work-around a deadlock issue the C# language authors came up with the solution: Task.ConfigureAwait(continueOnCapturedContext:false).

Besides a very strange name (that’s absolutely obscure when you see a method call without named argument) it does its job: it forces the continuation to run without synchronization context.

In this case the continuation of Task.Delay(1) task (empty statement in this case), is scheduled in a thread pool’s thread instead of the UI thread, causing the deadlock to go away.

Detaching the synchronization context

I know that the ConfigureAwait is the de-facto way of dealing with this problem but I see a huge issue with it. Here is a small example:

Can you see the problem here? We’ve used ConfigureAwait(false) so everything should be fine. But not necessarily.

ConfigureAwait(false) returns a custom awaiter called ConfiguredTaskAwaitable and we know that the awaiter is used only when the task is not finished synchronously. It means that if _cache.InitializeAsync() is completed synchronously we still could have a deadlock.

To solve the deadlock problem every awaited task should be “decorated” with a ConfigureAwait(false) call. Annoying and error-prone.

An alternative solution is to use a custom awaiter in every public method to detach the synchronization context from the asynchronous method:

Awaiters.DetachCurrentSyncContext returns the following custom awaiter:

DetachSynchronizationContextAwaiter does the following: if the async method runs with non-null synchronization context, the awaiter detects this and schedules the continuation to the thread pool’s thread. But if the async method runs without synchronization context IsCompleted property returns true and the continuation of the method runs synchronously.

It means that there is close-to-0 overhead if the async method runs from the thread pool’s thread and you pay only once to move the execution from the “UI”-thread to a thread pool’s thread.

Other benefits of this approach.

  • The approach is less error prone. ConfigureAwait(false) only works when all the awaited tasks are decorated with it. If you forget just one, the deadlock could happen. In the custom awaiter case, you should remember one thing: all the public methods of your library should start with Awaiters.DetachCurrentSyncContext(). Still possible to mess up, but the likelihood is lower.
  • The resulting code is more declarative and cleaner. In my opinion, a method with several ConfigureAwait calls is harder to read because of the noise, and the code is way less descriptive for a newcomer.

Exception handling

What’s the difference between this two cases:

The first case does exactly what you’d expect — handles the error, but the second one — doesn’t. TPL was designed for asynchronous and parallel programming and Task/Task<T> can represent a result of multiple operations. That’s why Task.Resultand Task.Wait() always throws an AggregateException that potentially contains more than one error.

But our main scenario changes everything: the user should be able to add async/await without changing the error handling logic. This means that await statement should be different from Task.Result/Task.Wait(): it should unwrap one exception from the AggregateException instance. Today it picks the first one.

Everything is fine if all the task-based methods are asynchronous and the tasks are not backed by a parallel computation. But this is not true all the time:

Task.WhenAll returns a task that fails with two errors, but await statement extracts and propagates just the first one.

There are two ways to solve this issue:

  1. Observe individual tasks manually if you have an access to them or
  2. Force TPL to wrap the exception into another AggregateException.

Async void methods

The task-based method returns a promise — a token that can be used for processing the results in the future. If the task is lost the promise becomes unobservable by a user’s code. The asynchronous operation that returns void makes the error case impossible to handle from the user’s code. This makes them kind-of useless and, as we’ll see in a moment — dangerous. But our main scenario makes them necessary:

But what will happen if GetStockPricesForAsync fail with an error? The unhandled exception of the async void method is marshaled to a current synchronization context triggering the same behavior as it was for the synchronous code (see ThrowAsync method at AsyncMethodBuilder.cs for more details). In Windows Forms an unhandled exception in the event handler triggers Application.ThreadException event, for WPF – Application.DispatcherUnhandledException event etc.

But what if the async void method doesn’t have a captured synchronization context? In this case, an unhandled exception will crash the app without an ability to recover from it. It won’t trigger recoverable [TaskScheduler.UnobservedTaskException] event, it will trigger unrecoverable AppDomain.UnhandledException event and will close the app. This is intentional and this is what it should be.

Now you should understand another famous best practice: Use async-void methods only for UI even handlers.

Unfortunately, it is relatively easy to accidentally introduce an async void method without realizing it.

It is very hard to tell just by looking at the lambda expression whether the function is task-based or async void and the error can easily sneak-in into your codebase even with a thorough code review.


There is one user scenario — simple migration from synchronous to asynchronous code for existing UI application — that affected asynchronous programming in C# in so many ways:

  • The continuations of async methods are scheduled into a captured synchronization context that can cause deadlocks.
  • To avoid deadlocks all the async library code should be littered with ConfigureAwait(false) calls.
  • await task; throws the first error making exception handling for parallel programming more complicated.
  • Async void methods were introduced for handling UI events but they can be used accidentally causing the application to crash in case of an unhandled exception.

There is no such thing as a free lunch. Ease of use in one case can complicate other use cases drastically. Knowing the history of asynchronous programming in C# makes the weird behaviors less weird and should reduce the likelihood of an error in your asynchronous code.


Leave a comment