How a Missing CancellationToken in ASP.NET Core Caused Thread Pool Exhaustion
A developer discovered a subtle but serious performance bug while reviewing a .NET Core application that was experiencing mysterious latency spikes under heavy load. The application used a fire-and-forget pattern where an API endpoint returned a '202 Accepted' response immediately while offloading heavy work to a background task. Because no CancellationToken was passed to the background task, it continued running even after clients disconnected or load balancers timed out, with no mechanism to stop early. At peak traffic of around 1,000 requests per second, concurrent background tasks saturated the HttpClient connection pool and queued CPU-bound work, eventually exhausting the thread pool entirely. The fix involved both propagating the CancellationToken from the original request into the background task and actively checking it during execution to allow early termination.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in