A slow request does not tell you whether work is waiting for a free worker or waiting on a dependency that can never complete. Diagnose the missing resource before changing thread counts. This tutorial compares both failures with bounded C# console programs.
Verified 1 November 2026 on Linux x64, targeting .NET 10 with SDK 10.0.402 and runtime 10.0.13 in a pinned offline container. The examples use only the BCL. Runtime and diagnostic-tool behavior can differ across versions and platforms.
Separate worker availability from a dependency cycle
The managed thread pool executes queued work using a limited number of workers. If available workers are occupied, new work can wait even though its inputs are ready. A deadlock instead involves dependencies that prevent progress; Microsoft’s threading guidance illustrates the classic case with two locks.
| Question | Worker starvation | Circular dependency |
|---|---|---|
| What is missing? | An available worker for ready work. | A prerequisite that another participant in the cycle must produce. |
| Can another worker help? | It can run queued work if capacity is the constraint. | It cannot satisfy the cycle by itself. |
| What evidence distinguishes them? | Queued work plus occupied workers and evidence of what keeps them busy. | A dependency graph showing which participant waits for which other participant. |
These are diagnostic questions, not mutually exclusive labels: a dependency cycle that blocks pool workers can also reduce capacity for unrelated work. The examples below isolate each mechanism so you can see the difference.
Reproduce worker unavailability, then remove the blocking wait
Create a .NET 10 console project. Replace Program.cs with this complete program and run it with dotnet run. Run it in a disposable console process, because it temporarily changes process-wide thread-pool limits.
The lab sets both the worker minimum and maximum to the greater of two and Environment.ProcessorCount. Matching the minimum to the cap lets the pool create workers on demand instead of making startup depend on gradual thread injection. SetMaxThreads can reject unsupported limits, including limits below the processor count or configured minimum. Every configuration call is checked, and the original limits are restored. This cap makes the experiment repeatable; it is not a production tuning recommendation.
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static readonly TimeSpan Limit = TimeSpan.FromSeconds(5);
static void Require(bool condition, string message)
{
if (!condition) throw new InvalidOperationException(message);
}
static TaskCompletionSource NewSignal() =>
new(TaskCreationOptions.RunContinuationsAsynchronously);
static Task QueueProbe()
{
var done = NewSignal();
Require(ThreadPool.QueueUserWorkItem(_ => done.SetResult()),
"Could not queue probe.");
return done.Task;
}
static void BlockingRound(int workers)
{
using var entered = new CountdownEvent(workers);
using var release = new ManualResetEventSlim();
var done = Enumerable.Range(0, workers).Select(_ => NewSignal()).ToArray();
Task probe = Task.CompletedTask;
try
{
foreach (var signal in done)
{
Require(ThreadPool.QueueUserWorkItem(_ =>
{
try
{
entered.Signal();
if (!release.Wait(Limit)) throw new TimeoutException("Worker wait.");
signal.SetResult();
}
catch (Exception error) { signal.SetException(error); }
}), "Could not queue worker.");
}
Require(entered.Wait(Limit), "Workers did not enter.");
probe = QueueProbe();
Require(!probe.IsCompleted, "Probe ran while every worker was blocked.");
}
finally
{
release.Set();
Require(Task.WaitAll(done.Select(x => x.Task).Append(probe).ToArray(), Limit),
"Blocking round did not drain.");
}
}
static async Task WaitForGate(Task gate, CancellationToken token)
{
await gate.WaitAsync(token);
}
static void AsyncRound()
{
var gate = NewSignal();
using var cancellation = new CancellationTokenSource();
var waiters = Enumerable.Range(0, 100)
.Select(_ => WaitForGate(gate.Task, cancellation.Token)).ToArray();
try
{
Require(QueueProbe().Wait(Limit), "Probe could not run.");
Require(waiters.All(x => !x.IsCompleted), "Gate unexpectedly opened.");
gate.SetResult();
Require(Task.WaitAll(waiters, Limit), "Async waiters did not drain.");
}
finally
{
cancellation.Cancel();
Require(Task.WhenAll(waiters).ContinueWith(_ => { },
TaskScheduler.Default).Wait(Limit), "Cleanup did not finish.");
}
}
static void CancellationRound()
{
var gate = NewSignal();
using var cancellation = new CancellationTokenSource();
Task waiter = WaitForGate(gate.Task, cancellation.Token);
cancellation.Cancel();
try { waiter.GetAwaiter().GetResult(); }
catch (OperationCanceledException) { }
Require(waiter.IsCanceled, "Cancellation was not observed.");
Require(!gate.Task.IsCompleted, "Canceling a wait changed the underlying gate.");
gate.SetResult();
}
static void Main()
{
ThreadPool.GetMinThreads(out int oldMin, out int ioMin);
ThreadPool.GetMaxThreads(out int oldMax, out int ioMax);
int workers = Math.Max(2, Environment.ProcessorCount);
Require(ThreadPool.SetMinThreads(workers, ioMin), "Minimum change rejected.");
try
{
Require(ThreadPool.SetMaxThreads(workers, ioMax), "Maximum change rejected.");
Console.WriteLine($"Visible processors: {Environment.ProcessorCount}");
for (int round = 0; round < 25; round++) BlockingRound(workers);
Console.WriteLine("Blocking: probe queued until workers released (25 rounds)");
for (int round = 0; round < 25; round++) AsyncRound();
Console.WriteLine("Async: probe ran with 100 pending waits (25 rounds)");
CancellationRound();
Console.WriteLine("Cancellation: waiter canceled; underlying gate unchanged");
}
finally
{
Require(ThreadPool.SetMaxThreads(oldMax, ioMax), "Maximum restore failed.");
Require(ThreadPool.SetMinThreads(oldMin, ioMin), "Minimum restore failed.");
}
}
}
Visible processors: 1
Blocking: probe queued until workers released (25 rounds)
Async: probe ran with 100 pending waits (25 rounds)
Cancellation: waiter canceled; underlying gate unchanged
Read the assertions, not elapsed time
The shown output records the container’s one visible processor. That number will reflect your own machine when you run the program. A timeout or an unhandled exception is a failed lab check, not proof of a deadlock. The process is disposable; if cleanup also fails, it can obscure the original failure, so preserve the complete error output rather than diagnosing from the final message alone.
In BlockingRound, every allowed worker signals that it has entered, then waits on the same closed event. The main thread is the coordinator and remains outside the pool. Only after all workers have entered does it queue a probe. With all workers occupied, that probe must remain incomplete. The coordinator opens the event in finally and joins the workers and probe, with five-second bounds.
There is no cycle between the workers: none needs another worker’s result before releasing its own resource. The external coordinator can open their gate. That is the key difference from the circular dependency in the next example. The forced cap demonstrates worker unavailability; it does not reproduce the default pool’s adaptive growth under real service load.
In AsyncRound, 100 operations await a task-backed gate. They return pending tasks instead of holding workers while the gate is closed. The same pool can execute a separately queued probe before any gate waiter completes. The program then opens the gate and observes every waiter. Both versions run 25 rounds; this is a correctness check, not a throughput benchmark. Microsoft’s async programming guidance explains why awaiting asynchronous I/O avoids occupying a thread for the wait.
TaskCompletionSource supplies a task that this program completes explicitly; it does not create an I/O operation. See the API documentation. A task-backed gate is useful here because the coordinator can control precisely when a prerequisite becomes available.
The cancellation check also distinguishes the wait from its underlying operation. Canceling WaitAsync cancels this wait; the gate remains incomplete until explicitly opened. A real I/O operation needs its own supported cancellation token and ownership policy. Cancellation is cooperative: observe completion before releasing resources that work may still use.
Compare a cycle that more workers cannot fix
Replace the first program with this second complete program. It creates two tasks: the first needs the second to finish, and the second needs the first. Neither task blocks a worker while suspended. The start signal uses the default continuation behavior; both task variables are assigned before it is opened.
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main()
{
var start = new TaskCompletionSource();
using var stop = new CancellationTokenSource();
Task? first = null;
Task? second = null;
async Task First()
{
await start.Task;
await second!.WaitAsync(stop.Token);
}
async Task Second()
{
await start.Task;
await first!.WaitAsync(stop.Token);
}
first = First();
second = Second();
start.SetResult();
if (first.IsCompleted || second.IsCompleted)
throw new InvalidOperationException("Expected a circular wait.");
Console.WriteLine("Cycle: first needs second; second needs first");
stop.Cancel();
Task joined = Task.WhenAll(first, second);
try { joined.WaitAsync(TimeSpan.FromSeconds(5)).GetAwaiter().GetResult(); }
catch (OperationCanceledException) { }
if (!first.IsCanceled || !second.IsCanceled)
throw new InvalidOperationException("Cancellation did not drain both tasks.");
Console.WriteLine("Cleanup: both tasks canceled and observed");
}
}
Cycle: first needs second; second needs first
Cleanup: both tasks canceled and observed
After the start signal, following either task’s dependency leads back to the same task. An idle worker cannot invent a completed prerequisite. This is a deliberately cancellable async dependency cycle: cancellation breaks both waits, and the program observes their canceled completion within a five-second bound. Without that escape, the dependencies have no route to normal completion. The assertion verifies both tasks’ final states, rather than treating a timeout alone as proof of deadlock. An unhandled TimeoutException means this cleanup check failed.
For a captured-context cycle, continue with diagnosing an async deadlock caused by blocking. For resource acquisition cycles, see reproducing a two-lock deadlock and fixing lock ordering. Those require reasoning about context ownership or lock order in addition to worker availability.
Apply the distinction to a running service
Collect evidence while the problem is occurring. Microsoft’s starvation diagnostic walkthrough applies to .NET 9 and later. Start with dotnet-counters: compare dotnet.thread_pool.thread.count, dotnet.thread_pool.queue.length, the rate of change of dotnet.thread_pool.work_item.count, and CPU utilization over the same interval. Rising worker counts with low CPU can suggest blocking; a large queue is common but not required. A single sample is insufficient.
Use dotnet-stack for persistent stalls, or dotnet-trace for intermittent ones. Inspect blocked call stacks and trace dependencies back to the operation that should release each wait. The WaitHandleWait event is available starting in .NET 9. It can locate blocking waits, but a wait event alone does not establish a cycle. .NET 6 introduced faster pool compensation for some blocking Task APIs, so do not expect this capped lab’s behavior from the default pool.
Choose a correction that addresses the dependency: use an asynchronous call chain for asynchronous waits, change inconsistent lock ordering for a lock cycle, or remove a captured-context blocking cycle. Then rerun the same bounded reproduction, verify successful completion and cancellation, and check that every started operation is observed. Avoid using a thread-count change as proof that the underlying problem is understood.