Skip to content

2.13 — Deadlocks: .Result, .Wait() and ConfigureAwait(false)

← Roadmap · Section: 2. Collections, LINQ and Async Prev: 2.12 Task vs ValueTask · Next: 2.14 CancellationToken

Markers: 🔥 ⚠️ · Confidence: 🔴 · Last reviewed: —


TL;DR

  • Never call .Result or .Wait() on a task. In some apps it deadlocks permanently; in ASP.NET Core it wastes a thread and can bring the server down under load.
  • The classic deadlock happens when a thread blocks waiting for work that needs that same thread to finish.
  • ASP.NET Core has no synchronization context, so it cannot deadlock this way — but blocking is still harmful, and ConfigureAwait(false) still matters in libraries.

The concept

A deadlock is two things each waiting for the other, so neither can ever proceed.

The async version is subtler and more absurd: one thread waiting for itself.

A picture: you are the only person working in a shop. A customer asks you to fetch something from the stockroom. You say "I'll wait right here at the till until someone brings it out" — but you are the only one who can go and get it. You will stand there forever.

That is exactly what .Result does in an app with a synchronization context. The thread blocks waiting for the async work to complete, but the async work's continuation is queued to run on that very thread, which is busy blocking. Nothing can ever move.


How it works

What a synchronization context is

Some frameworks require certain code to run on a specific thread. A desktop UI is the classic example — only the UI thread may touch controls, or the framework throws.

The synchronization context is the mechanism that enforces this. When you await in such an app, the continuation is not run on just any thread; it is posted back to the context so it resumes on the original thread.

Framework Has a synchronization context?
WinForms / WPF ✅ Yes — the UI thread
ASP.NET (old, System.Web) ✅ Yes — the request context
ASP.NET Core ❌ No
Console apps ❌ No

The classic deadlock

// In a WinForms button handler, or old ASP.NET
public void Button_Click(object sender, EventArgs e)
{
    var data = GetDataAsync().Result;      // ← DEADLOCK
    label.Text = data;
}

private async Task<string> GetDataAsync()
{
    await Task.Delay(100);                 // continuation wants the UI thread
    return "done";
}

Step by step:

  1. Button_Click runs on the UI thread.
  2. .Result blocks the UI thread, waiting.
  3. Inside, await Task.Delay(100) captures the synchronization context.
  4. After 100 ms the delay finishes, and the continuation — return "done" — is posted to the UI thread.
  5. The UI thread is blocked at step 2 and cannot run it.
  6. The task never completes, so .Result never returns. Frozen forever.

The application hangs with no exception and no stack trace pointing anywhere useful.

What ConfigureAwait(false) does

It says: "I do not need to resume on the original context. Any thread will do."

private async Task<string> GetDataAsync()
{
    await Task.Delay(100).ConfigureAwait(false);   // resume on a pool thread
    return "done";
}

Now the continuation runs on a thread-pool thread, the task completes, and .Result returns. The deadlock is gone.

But this is a workaround, not a fix. The real fix is not to block:

public async void Button_Click(object sender, EventArgs e)   // async void OK for handlers
{
    var data = await GetDataAsync();       // no blocking, no deadlock
    label.Text = data;
}

ASP.NET Core: no deadlock, but still a serious problem

ASP.NET Core removed the synchronization context deliberately. So this does not deadlock:

public IActionResult Get()
{
    var data = _service.GetDataAsync().Result;   // no deadlock in ASP.NET Core
    return Ok(data);
}

It still must not be done. Here is why:

The whole point of async is that a thread is released while waiting (2.10). .Result throws that away — the thread sits blocked for the entire duration of the I/O.

Under load that becomes thread-pool starvation:

  1. 100 requests arrive, each blocking a pool thread on a 200 ms database call.
  2. All available pool threads are blocked. None can pick up new work.
  3. The pool does add threads — but slowly, roughly one or two per second by design.
  4. Meanwhile requests queue up. Latency goes from 200 ms to 30 seconds.
  5. Health checks time out. The orchestrator restarts the container. Traffic shifts to the remaining instances, which then fall over the same way.

The failure looks like a total outage, and CPU usage is near zero — because every thread is sitting doing nothing. That signature, high latency with idle CPU, is the classic fingerprint of this bug and a great thing to be able to name in an interview. See 19.12.

So do you need ConfigureAwait(false) in ASP.NET Core?

Where Needed?
Application code (controllers, services in your web app) No. There is no context to capture, so it changes nothing.
Library code (a NuGet package, shared code) Yes. Your library might be consumed by a WinForms app that does have a context.

So in an ASP.NET Core project, adding .ConfigureAwait(false) everywhere is noise. In a reusable library, it is correct practice.

.NET 8 added ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing) and other options, but the false/true distinction above is what gets asked about.


Code

The three ways this appears in real code

// 1. Blocking on a task
var user = _service.GetUserAsync(id).Result;
_service.SaveAsync(user).Wait();

// 2. Sync-over-async wrapper — a whole method that exists to block
public User GetUser(Guid id) => GetUserAsync(id).GetAwaiter().GetResult();

// 3. Blocking inside a constructor, because constructors cannot be async
public MyService()
{
    _config = LoadConfigAsync().Result;      // ← very common
}

The fixes

// 1 & 2 — go async all the way up
public async Task<IActionResult> Get(Guid id, CancellationToken ct)
{
    var user = await _service.GetUserAsync(id, ct);
    return Ok(user);
}
// 3 — constructors cannot be async, so do not do work in them.
// Use an async factory, or let DI resolve it.
public class MyService
{
    private Config? _config;

    private MyService() { }

    public static async Task<MyService> CreateAsync(CancellationToken ct)
    {
        var service = new MyService();
        service._config = await LoadConfigAsync(ct);
        return service;
    }
}

In ASP.NET Core specifically, the better answer for startup work is IHostedService or the options pattern — see 7.6 and 21.1.

If you genuinely cannot avoid blocking

Sometimes you are in a legacy interface you cannot change. There is no safe answer, only a less unsafe one:

// Runs the async work on a fresh context-free thread, then blocks on that.
var result = Task.Run(() => GetDataAsync()).GetAwaiter().GetResult();

This avoids the deadlock because Task.Run moves the work off the captured context. It still blocks a thread. Treat it as a documented last resort, not a pattern.


Interview questions

Q: Why does calling .Result on a task sometimes deadlock?

Because the thread blocks waiting for the task, while the task's continuation is queued to run on that same blocked thread. In a UI application or old ASP.NET, await captures the synchronization context so the code after it resumes on the original thread. If that thread is blocked inside .Result, the continuation can never run, the task never completes, and the block never ends. It is effectively one thread waiting for itself.

Q: Does that happen in ASP.NET Core?

No, because ASP.NET Core has no synchronization context — continuations run on any available thread-pool thread, so there is nothing to deadlock against. But blocking is still a serious problem there for a different reason. It defeats the purpose of async by pinning a thread for the whole duration of the I/O, and under load that causes thread-pool starvation: all pool threads end up blocked, the pool only injects new ones at about one or two per second, and latency collapses. The distinctive symptom is very high latency with almost no CPU usage.

Q: What does ConfigureAwait(false) do, and do you need it?

It tells the runtime not to capture the current synchronization context, so the continuation can resume on any thread rather than being posted back to the original one. That is what prevents the classic deadlock. In ASP.NET Core application code it does nothing, because there is no context to capture, so adding it everywhere is just noise. It genuinely matters in library code, because you do not control who consumes your library — it might be a desktop app that does have a context, and without it your library could deadlock their application.

Q: How would you diagnose thread-pool starvation in production?

The signature is high request latency combined with low CPU — the threads are blocked, not busy. I would check the ThreadPool queue length and thread count via dotnet-counters, where you would see the queue growing and the thread count climbing slowly. Then I would take a dump with dotnet-dump and look at the stacks; starvation shows up as many threads all sitting in Monitor.Wait or Task.Result. The fix is to find the sync-over-async call and make the path async all the way up.


Traps ⚠️

  • .Result, .Wait(), and .GetAwaiter().GetResult() all block. All three are the same problem.
  • It works in development and fails in production. With one user there are plenty of free threads; the failure only appears under concurrency.
  • ConfigureAwait(false) in ASP.NET Core app code does nothing. It is not a fix for blocking, and it is not needed there.
  • In libraries, omitting ConfigureAwait(false) can deadlock your consumers.
  • Constructors cannot be async, which is why people block in them. Use an async factory instead.
  • .Result wraps exceptions in AggregateException, so your catch (SqlException) will not match. See 2.16.
  • One blocking call deep in a library can starve the whole application. The problem is not local to where it is written.


Sources