ThreadPool sizing in .NET 10: minimum threads, hill climbing, starvation-based injection, configuration options, why higher limits mask blocking code, and the narrow cases where tuning is justified.
The ThreadPool creates threads immediately up to MinThreads (default: the processor count); above that it adds them slowly, using hill climbing to find the count that maximises throughput and starvation detection to add threads when queued work isn't progressing.
SetMinThreads (or System.Threading.ThreadPool.MinThreads in runtimeconfig.json) just raises that instant-creation threshold. If requests are slow because threads are blocked on .Result, .Wait() or sync I/O, more threads let more requests block in parallel: latency improves briefly, memory and context switching grow, and the real bug stays.
Tuning is legitimate for known bursts and unavoidable blocking (a legacy synchronous SDK), as a documented stop-gap. The trade-off: it buys time, not scalability.
"The ThreadPool has a minimum, which defaults to the number of processors the runtime sees, including container limits. Up to that minimum, when work is queued and no thread is free, it creates a thread immediately. Above it, it's deliberately conservative: hill climbing measures throughput as it varies the thread count and keeps the count that performs best, and a starvation detector injects extra threads, historically around one or two a second, when the queue isn't draining. Since .NET 6, blocking on a task also signals the pool so it injects faster, but it's still a mitigation.
For example, at [company] we had [an API] where a team set
MinThreadsto [500] after a starvation incident. It looked fixed, but the root cause was a synchronous Redis call wrapped in.Resulton every request. Under a traffic spike we hit [480] threads, an extra [500 MB] of stack memory reserved, heavy context switching and p99 worse than before. I took a dump, showed [400] threads parked in.Result, and we made the call path async end to end. We then set the minimum back to the default and the service ran at about [40] threads with lower latency.The mistake I see is treating thread count as a capacity knob. In an async ASP.NET Core service, a few dozen threads should serve thousands of concurrent requests. If you need hundreds, something is blocking. I'll raise the minimum only for a known burst pattern or a genuinely synchronous dependency I can't change yet, and I write down why and when it comes out."
| Mechanism | What it does | Speed |
|---|---|---|
| Minimum threads | Create on demand without delay | Instant up to the minimum |
| Hill climbing | Varies count, keeps the throughput-maximising level | Gradual, continuous |
| Starvation detection | Adds threads when queued items don't progress | Roughly 1–2 per second historically; faster for blocking on tasks since .NET 6 |
| Maximum threads | Upper bound (very high by default) | Rarely the limit |
| Option | Scope |
|---|---|
ThreadPool.SetMinThreads(worker, io) |
In code, at start-up |
"System.Threading.ThreadPool.MinThreads": 64 in runtimeconfig.template.json |
Build-time config |
DOTNET_ThreadPool_ForceMinWorkerThreads env var (hex) |
Deployment-time |
| Situation | Tune? |
|---|---|
| Sync-over-async in your own code | No: fix the code |
| Unavoidable synchronous third-party SDK on a hot path | Temporarily, with a plan to isolate or replace |
| Known start-up burst (cache warm, connection storms) | Possibly, a modest increase |
| CPU-bound work | No: more threads than cores adds context switches |
With MinThreads at [500] and sync Redis calls, p99 under a spike was [3.2 s] with [480] threads. After making the path async and restoring defaults, the same spike ran on about [40] threads with p99 [260 ms] and [450 MB] less memory.
Checks whether the candidate understands how the ThreadPool really sizes itself and treats thread-count tuning as a symptom fix rather than a solution.
Green flags
Red flags
It runs queued work items (continuations, Task.Run, timer callbacks, I/O completions) on a set of worker threads, keeping enough threads to use the CPUs without oversubscribing them. Since .NET 6 the pool is implemented in managed code on every platform, and since .NET 7 Windows I/O completions are also processed by it.
queues (global + per-thread local) --> worker threads (N) --> CPU cores (C)
^ |
|-- continuations, Task.Run <-----|
Environment.ProcessorCount, which honours container CPU limits (a pod limited to 2 CPUs gets 2). Up to this many busy threads, a new work item gets a new thread at once.Task.Wait/.Result on an incomplete task tells the pool a thread is blocking, so it can add threads more aggressively. It shortens the pain, not the cause.Each blocked thread holds a stack (reserved 1.5 MB on Linux x64 by default, 1 MB on Windows), shows up in GC root scanning, and adds context switches when it wakes. Raising the minimum lets more requests block concurrently, so the queue drains for a while, but throughput per thread stays tiny and the system falls over at the next multiple of load.
dotnet-counters: threadpool-thread-count, threadpool-queue-length, threadpool-completed-items-count.dotnet-dump analyze with threadpool and clrstack -all (or parallelstacks) shows what the threads are blocked on.Always record it in an ADR or a code comment with the measurements and the removal condition.
Task.Delay(100).Wait(); load it with 200 VUs and graph threadpool-thread-count and queue length.System.Threading.ThreadPool.MinThreads to 200 and repeat; note memory and p99.--cpus=2 and print Environment.ProcessorCount and ThreadPool.GetMinThreads.Inspecting pool settings, the blocking anti-pattern that tempts people to tune, and the fix.
// Program.cs (.NET 10)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient<InventoryClient>();
var app = builder.Build();
// Visible at start-up: what the pool will create instantly.
ThreadPool.GetMinThreads(out var minWorker, out var minIo);
app.Logger.LogInformation("ProcessorCount={Cpus} MinWorker={Min} MinIo={MinIo}",
Environment.ProcessorCount, minWorker, minIo);
// BAD: blocks a pool thread per request. Raising MinThreads only lets more of these block at once.
app.MapGet("/stock-blocking/{sku}", (string sku, InventoryClient client) =>
Results.Ok(client.GetStockAsync(sku, CancellationToken.None).Result));
// GOOD: no thread held while waiting for the network.
app.MapGet("/stock/{sku}", async (string sku, InventoryClient client, CancellationToken ct) =>
Results.Ok(await client.GetStockAsync(sku, ct)));
app.Run();
public sealed class InventoryClient(HttpClient http)
{
public async Task<int> GetStockAsync(string sku, CancellationToken ct) =>
await http.GetFromJsonAsync<int>($"https://inventory.internal/stock/{sku}", ct);
}
// runtimeconfig.template.json: only with a documented reason and a removal plan
{
"configProperties": {
"System.Threading.ThreadPool.MinThreads": 32
}
}
dotnet-counters monitor -n MyApi --counters System.Runtime[threadpool-thread-count,threadpool-queue-length,cpu-usage]
MinThreads to hundreds to 'fix' starvation. Why it is wrong: It hides sync-over-async and adds stack memory and context switching; with hundreds of runnable threads, short tasks such as health checks and timer callbacks wait behind them, so responsiveness drops unevenly. Instead: Remove .Result, .Wait() and sync I/O.Environment.ProcessorCount in the pod and size CPU limits with this in mind.MaxThreads. Why it is wrong: The maximum is rarely the limit; injection rate is. Instead: Leave it alone.A senior treats ThreadPool tuning as a stop-gap with an expiry date. The pool's defaults are tuned for async code; if a service needs far more threads than cores, the design is blocking somewhere. The better structural answers are making the path async, isolating unavoidable blocking work behind a bounded dedicated scheduler or a separate worker, and bounding concurrency so bursts queue rather than spawn threads.
| Option | When it fits | Cost | When not to |
|---|---|---|---|
| Fix blocking code | Any sync-over-async | Engineering time | Never skip it |
| Modest MinThreads increase | Known bursts; temporary vendor blocking | Memory, masks regressions | As the permanent fix |
| Dedicated threads for a sync SDK | Unavoidable blocking dependency | Separate capacity to size | When an async SDK exists |
| Bounded concurrency (SemaphoreSlim, rate limiter) | Bursty inbound work | Queueing latency | Latency-critical paths that can't queue |
The ThreadPool is a process-wide set of reusable background threads that run queued work items: Task.Run, async continuations, timer callbacks and ASP.NET Core requests. Work goes into a global queue or, when queued from a pool thread, into that thread's local queue, and idle threads steal from others. The pool starts up to minimum threads (default: processor count) immediately on demand; above that it adds threads gradually, using a hill-climbing algorithm that measures throughput plus a starvation detector that injects threads when queued work is not progressing. That gradual injection is why blocking pool threads causes latency spikes.
ThreadPool starvation happens when pool threads are blocked faster than the pool can add new ones, so queued work (requests, continuations, timers) waits. Symptoms: latency rises sharply while CPU stays low, the pool queue length grows, thread count climbs slowly, health checks time out and Kestrel logs heartbeat warnings mentioning possible starvation. I confirm it with dotnet-counters (queue length and thread count), then take dotnet-stack snapshots or a dump to see what the threads are blocked on, usually .Result, .Wait(), synchronous I/O or locks. The fix is to make that code asynchronous; raising SetMinThreads only buys time.
Sync-over-async is synchronously blocking on asynchronous work: .Result, .Wait(), GetAwaiter().GetResult() or Task.WaitAll. It holds a thread for the whole operation, causes ThreadPool starvation on servers and deadlocks under a SynchronizationContext. It sneaks in through constructors, DI factory lambdas, property getters, Dispose, lock blocks, synchronous interfaces (IComparer, old filters), static initialisers and "quick fixes" in legacy code. Remove it by making the call chain async, using async factory methods or lazy async initialisation, IAsyncDisposable, hosted-service warm-up, and async-capable interfaces. Where a sync boundary is unavoidable (for example a legacy interface), keep it at the edge and document it.
How to tell it (STAR)
**S:** At [company] an earlier starvation incident had been 'fixed' by setting MinThreads to [500]. During a [campaign] traffic tripled and p99 went to [3 seconds] with pods being OOMKilled. **T:** I led the investigation. **A:** A dump showed [400] threads blocked on .Result in our Redis wrapper, so the setting had only hidden it. I made the cache path async, proved with a k6 run that default pool settings handled [3x] load, removed the override and wrote an ADR that any future override needs a root-cause ticket. **R:** The next spike ran on about [40] threads per pod with p99 [260 ms] and [450 MB] less memory per pod.
Last updated 23 Nov 2026.