skip to content

In an ASP.NET Core 10 endpoint, why is calling .Result or .Wait() on a Task harmful, and what should you write instead?

level: juniorimportance: must knowfreq 72%

answer

  1. a blocked thread serves nobody
  2. sync-over-async
  3. thread pool grows slowly
  4. async Task<IResult> all the way
  5. GetAwaiter().GetResult() is no better

basics

~20 s

Blocking on .Result or .Wait() parks a thread-pool thread until the awaited I/O finishes, so under load ASP.NET Core runs out of free threads and every request slows down. Make the handler async and await the call instead.

solid answer

~50 s

ASP.NET Core serves requests on a small pool of thread-pool threads. An `await` on I/O hands the thread back while the database or remote call is in flight; `.Result`, `.Wait()` or `.GetAwaiter().GetResult()` keep it **blocked** for the whole wait. That is **sync-over-async**: the blocked thread does nothing while another pool thread is still needed to finish the async work, so under load the pool runs out of free threads. The pool injects new threads only gradually, so queued requests wait, latency climbs from milliseconds to seconds, and CPU stays low. ASP.NET Core has no synchronization context, so the classic UI-style deadlock is not the main risk here; **thread-pool starvation** is. The fix is **async all the way**: make the handler `async`, return `Task<T>` or `Task<IResult>`, and `await` every I/O call down the stack. Wrapping the blocking call in `Task.Run` does not fix it.

code

csharp · 6 lines
csharp
app.MapGet("/reports/{id:int}", async Task<Results<Ok<FreightReport>, NotFound>> (
    int id, IReportService reports, CancellationToken ct) =>
{
    var report = await reports.BuildAsync(id, ct);
    return report is null ? TypedResults.NotFound() : TypedResults.Ok(report);
});

go deeper

for a junior

Recall that handlers doing I/O should be async and await their calls, and that .Result or .Wait() blocks a thread the server needs.

for a middle

Explain why blocking starves the thread pool under load, why ASP.NET Core does not deadlock the old way, and why Task.Run is not a fix.

for a senior

Trace sync-over-async through every layer, including libraries, and plan how to make a blocking call path async without a big-bang rewrite.

for a principal

Set the rule that request paths are async end to end and decide how unavoidable synchronous dependencies are isolated and capacity-tested.

## What the thread pool does for a request Kestrel accepts connections and hands each request to code that runs on **thread-pool threads**. ASP.NET Core does not create a thread per request; it relies on a small, shared pool and expects request code to give its thread back whenever it waits for I/O. That is exactly what `await` does. When a handler awaits a database query or an outgoing HTTP call, the method returns an incomplete `Task`, the thread goes back to the pool, and some other request can use it. When the I/O completes, the rest of the method is scheduled on any free pool thread. ## What .Result and .Wait() do instead **Sync-over-async** means calling an asynchronous API and then blocking until it finishes: - `task.Result` and `task.Wait()` block the calling thread; - `task.GetAwaiter().GetResult()` blocks just the same (it only changes how exceptions are wrapped); - synchronous wrappers in libraries that do this internally have the same effect. The blocked thread does nothing useful for the length of the I/O. In the **freight dashboard**, a report endpoint that calls `reports.BuildAsync(id).Result` holds a thread for every report being built. With 50 concurrent report requests, 50 pool threads are parked, and the async work they wait for also needs pool threads to complete. ## Why it gets worse under load | Load | What happens | |---|---| | One user | Looks fine: the block lasts as long as the query | | Moderate | The pool's existing threads are all blocked; new requests queue | | High | The pool adds threads only gradually; queues grow, latency climbs from milliseconds to seconds, CPU stays low | This is **thread-pool starvation**. It looks like a slow database or a slow network, but the database is idle: the requests are waiting for a thread, not for data. ASP.NET Core has **no `SynchronizationContext`**, so the classic deadlock from desktop or old ASP.NET apps (a continuation waiting for the very thread that is blocked on it) does not occur in the same form. Starvation is the failure mode to explain in an ASP.NET Core interview. ## The fix: async all the way 1. Change the handler signature to `async` and return `Task<T>`, `Task<IResult>` or `Task<Results<...>>`. 2. `await` the call instead of reading `.Result`. 3. Push the change down: every method between the endpoint and the I/O becomes async too. A single synchronous layer in the middle reintroduces the block. 4. Pass the request's `CancellationToken` down, so abandoned requests stop working. ```csharp // blocks a thread-pool thread for the whole query app.MapGet("/reports/{id}", (int id, IReportService reports) => TypedResults.Ok(reports.BuildAsync(id).Result)); // gives the thread back while the query runs app.MapGet("/reports/{id}", async (int id, IReportService reports, CancellationToken ct) => TypedResults.Ok(await reports.BuildAsync(id, ct))); ``` ## Fixes that are not fixes - **`await Task.Run(() => SyncCall())`**: the ASP.NET Core best-practice guidance says not to. It moves the blocking call to another pool thread, adds scheduling overhead and blocks a thread anyway. - **Raising the minimum thread count**: it can hide the symptom at one load level and fails at the next; the blocking is still there. - **`ConfigureAwait(false)` sprinkled around**: it changes where a continuation runs, which ASP.NET Core does not care about; it does not unblock `.Result`. ## How to spot it in code review Sync-over-async rarely looks like a bug in a diff. Look for: - `.Result`, `.Wait()`, `.GetAwaiter().GetResult()` or `Task.WaitAll` anywhere under a handler, filter or middleware; - constructors or property getters that call async services, because they cannot `await`; - synchronous interfaces implemented over async clients, such as a `GetReport()` method that wraps `GetReportAsync()`; - `lock` blocks around I/O, which serialize requests and block every waiting thread; - `async void` methods, which the framework cannot await at all. Analyzers and a team rule that hot paths stay `async` end to end catch most of these before they reach a load test. ## When blocking is unavoidable Sometimes a library offers only synchronous I/O. Then the honest options are to find an async alternative, isolate the call behind a bounded resource (a dedicated queue processed by a background service), or accept a capacity limit and load-test it. Blocking inside the request path should be a recorded decision, never an accident.

  • Does ASP.NET Core deadlock when you call .Result, like old ASP.NET did?
    Not in the same way. ASP.NET Core has no `SynchronizationContext`, so continuations do not need the blocked request thread and the classic single-thread deadlock does not occur. What does happen is thread-pool starvation: enough blocked threads leave no free ones, so requests queue and latency grows until the pool slowly adds threads.
  • Is task.GetAwaiter().GetResult() better than task.Result?
    Only in how exceptions surface: `GetResult()` rethrows the original exception, while `.Result` wraps it in an `AggregateException`. Both block the calling thread for the whole operation, so both cause the same starvation under load. The fix is to `await`.

A thread-pool thread is like a restaurant waiter: await is taking the order to the kitchen and serving other tables meanwhile; .Result is standing at the pass until that one dish is cooked while new guests wait at the door.

  • Calling .Result is fine in ASP.NET Core because there is no SynchronizationContext.
  • Wrapping the blocking call in await Task.Run makes the endpoint properly asynchronous.
  • GetAwaiter().GetResult() does not block the thread, unlike .Result.
  • async makes the handler run on a separate dedicated thread.
  • Adding ConfigureAwait(false) everywhere removes the need to make callers async.