skip to content

Under load, an ASP.NET Core 10 freight-dashboard report endpoint goes from 200 ms to several seconds while CPU stays low; how do you confirm thread-pool starvation and fix it?

level: seniorimportance: must knowfreq 55%

answer

  1. reproduce with a fixed-concurrency load test
  2. dotnet.thread_pool.thread.count creeping up
  3. queue length high, CPU low
  4. stacks parked in Task.Wait / GetResult
  5. fix the block, not MinThreads

basics

~20 s

Reproduce it under a load test, then watch the runtime's thread-pool metrics: thread count creeping up while CPU stays low and the queue grows. Thread stacks blocked in Wait or GetResult name the culprit. Fix that code path to await instead of blocking.

solid answer

~40 s

First **reproduce** it: a load test at the concurrency that hurts, measuring p95/p99 latency, not just the average. While it runs, read the runtime's thread-pool metrics; in .NET 9 and later the `System.Runtime` meter reports `dotnet.thread_pool.thread.count`, `dotnet.thread_pool.queue.length` and `dotnet.thread_pool.work_item.count`. The starvation signature is a **thread count that keeps creeping up** (a burst, then a thread or two per second), a growing queue, few completed work items and **CPU well below 100%**. To find the cause, capture thread stacks: many pool threads parked in `Task.Wait`, `GetResultCore` or `ManualResetEventSlim.Wait` under your report code point straight at sync-over-async. Then fix the code: await the call, make the layers between async, and replace synchronous body reads. Raising `ThreadPool.SetMinThreads` only buys headroom; rerun the same load test to prove the fix.

code

bash · 5 lines
bash
# live thread-pool and CPU metrics of the running report service
dotnet-counters monitor -n Freight.Dashboard

# one-off dump of every managed thread's stack
dotnet-stack report -n Freight.Dashboard

go deeper

for a junior

Know that a server can be slow with low CPU because threads are blocked, and that load tests are how you see it.

for a middle

Name the signals: creeping thread count, growing queue, low CPU, and the blocked stacks that point at .Result or Wait.

for a senior

Run the whole loop: reproduce with a fixed-concurrency load test, read the thread-pool metrics, capture stacks or wait events, fix the blocking path and prove it with the same test.

for a principal

Decide when a MinThreads mitigation is acceptable, how starvation signals feed alerting, and how load tests gate releases of hot endpoints.

## Recognise the shape of the problem Thread-pool starvation has a characteristic profile, and the scenario fits it: - latency is fine at low concurrency and collapses under load; - the database and downstream services look idle or normal; - CPU on the web servers is **well below 100%**; - restarting the app helps briefly. A compute-bound endpoint shows the opposite: CPU pinned. A slow dependency shows slow dependency calls in traces. Starvation shows requests **waiting to start**, because no thread is free to run them. ## Step 1: reproduce under a controlled load test You cannot diagnose a load problem without load. Use a load generator with **fixed concurrency** against a staging copy of the report endpoint, and record: 1. throughput (requests per second); 2. latency percentiles, especially p95 and p99; 3. error rate and timeouts. Run a baseline at low concurrency, then step up. A starving endpoint shows latency rising far faster than concurrency, while CPU stays low. Keep the test: it is also how you prove the fix. ## Step 2: read the thread-pool metrics In .NET 10 the runtime's `System.Runtime` meter exposes thread-pool instruments that `dotnet-counters` (and an OpenTelemetry metrics pipeline) can read: | Instrument | Healthy | Starving | |---|---|---| | `dotnet.thread_pool.thread.count` | stable | climbs fast to a few times the core count, then a thread or two per second | | `dotnet.thread_pool.queue.length` | near 0 | large and growing | | `dotnet.thread_pool.work_item.count` | high rate | low rate: little work completes | | CPU usage | proportional to load | well below 100% | The slow, steady growth in thread count is the giveaway: the pool keeps injecting threads because the existing ones are blocked, not busy. ## Step 3: find the blocking code Capture managed thread stacks while the problem is happening (for example with `dotnet-stack report`). Starvation shows dozens of pool threads with the same top frames: `ManualResetEventSlim.Wait`, `Task.SpinThenBlockingWait`, `Task<T>.GetResultCore`, with your code just below, such as `ReportService.Build()` calling `.Result`. For intermittent blocking, a trace of wait events (`WaitHandleWait`, available since .NET 9) records which call sites blocked and for how long. Common culprits in an ASP.NET Core app: - `.Result`, `.Wait()` or `.GetAwaiter().GetResult()` on async calls; - synchronous body access, such as `HttpContext.Request.Form` or `StreamReader.ReadToEnd()` with `AllowSynchronousIO` turned on; - serializing a lazily evaluated `IEnumerable<T>` backed by a database query; - `lock` or `SemaphoreSlim.Wait()` around slow work in a hot path; - a synchronous-only client library called from a handler. ## Step 4: fix the cause, then prove it Make the blocking path asynchronous end to end: `async` handler, `await` on every I/O call, `ReadFormAsync` instead of `Request.Form`, `ToListAsync` or `IAsyncEnumerable<T>` instead of a lazy query. Rerun the same load test and compare percentiles and the thread-count curve. ## Making the load test trustworthy A load test only proves something if it is repeatable: - use the same build configuration, instance size and data volume as production, or the numbers do not transfer; - warm the app up first, then measure a steady phase long enough to see the thread count settle or keep climbing; - drive **fixed concurrency** or a fixed arrival rate, and record percentiles, not just the mean; - keep the scenario in source control and rerun it after the fix and before later releases of the same endpoint. ## What not to do - **Raise the minimum thread count as the fix.** `ThreadPool.SetMinThreads`, the `ThreadPoolMinThreads` MSBuild property or `System.Threading.ThreadPool.MinThreads` in `runtimeconfig.json` give the pool a head start, which can be a short-term mitigation while the code is fixed. Each blocked thread still costs memory and context switches, and the next traffic peak finds the new limit. - **Scale out first and ask later.** More instances hide the problem and multiply cost. - **Blame the database** because the endpoint "waits on a query". Check whether the query itself is slow before tuning it.

  • Is raising ThreadPool.SetMinThreads ever the right move?
    As a temporary mitigation, yes: a higher minimum lets the pool start more threads immediately instead of injecting them gradually, which can stop an outage while the blocking code is fixed. It is not a fix: blocked threads still waste memory and scheduling time, and a larger traffic peak exhausts the new minimum. Remove it once the code path awaits properly, and confirm with the same load test.
  • Why does CPU stay low during thread-pool starvation?
    Because the threads are blocked, not working. Each one is parked waiting for a task or a lock, so the processor has little to run while requests sit in the queue. That low CPU combined with high latency and a creeping thread count is what separates starvation from a compute-bound endpoint.

  • Low CPU during a latency spike proves the bottleneck is the database.
  • Setting ThreadPool.SetMinThreads to a large value is the permanent fix for starvation.
  • The thread pool adds hundreds of threads instantly, so starvation cannot last long.
  • Average latency from a few manual requests is enough to confirm a throughput fix.
  • Adding more instances is the right first response to thread-pool starvation.

Assumes .NET 10; the dotnet.thread_pool.* instruments of the System.Runtime meter and the WaitHandleWait event are available since .NET 9, so older runtimes show differently named counters.