ASP.NET WebAPI: Why Is This Request Timing Out After 30 Seconds?
In plain English (30 sec): A focused deep-dive on a specific mechanism or problem pattern.
The symptom
“Our
/api/orders/recentendpoint returns 502 Bad Gateway after exactly 30 seconds when the site has more than about 20 concurrent users. Works fine in dev with 1-2 users.”
The 502 comes from the load balancer (Azure App Service’s built-in load balancer or AWS ALB), which has a 30-second idle timeout. The endpoint isn’t returning anything inside that window — so the proxy gives up.
Reproduce locally
// GET /api/orders/recent?count=50
[HttpGet("recent")]
public async Task<IActionResult> GetRecentOrders([FromQuery] int count = 50)
{
// Simulates the slow operation users report
var orders = await _db.Orders
.OrderByDescending(o => o.CreatedAt)
.Take(count)
.ToListAsync();
return Ok(orders);
}
Start the app, hit the endpoint with 25 concurrent requests:
# using hey (formerly rakyll/hey)
hey -n 100 -c 25 http://localhost:5000/api/orders/recent
Watch: a handful complete quickly, then everything stalls until the 30-second Kestrel timeout kills the connections.
The root cause chain
1. ToListAsync blocks the thread
Look at the action method more carefully — the real code wasn’t using ToListAsync:
[HttpGet("recent")]
public async Task<IActionResult> GetRecentOrders([FromQuery] int count = 50)
{
var orders = _db.Orders
.OrderByDescending(o => o.CreatedAt)
.Take(count)
.ToList(); // SYNC! Not ToListAsync
return Ok(orders);
}
The method is async Task<IActionResult> but calls the synchronous .ToList() instead of .ToListAsync(). The method signature is misleading: the method appears async, but the body has a blocking call.
2. Thread pool starvation
ASP.NET Core serves requests using the .NET thread pool. When you call .ToList(), the Entity Framework provider sends a SQL query to SQL Server and blocks the current thread waiting for the response. That thread cannot serve any other request while it waits.
With 25 concurrent users and only 8-16 thread pool threads available per core, every thread gets blocked on a database query. New requests are queued. Queries take longer because SQL Server is under load from 25 simultaneous query executions instead of a few interleaved async ones.
The thread pool attempts to inject new threads at a rate of about 1-2 per second (the thread injection algorithm adds one thread every 500ms once threads are saturated). By the time new threads arrive to process the waiting requests, the 30-second load balancer timeout has already fired.
3. Confirm with counters
# Watch thread pool state
dotnet counters monitor --process-id <pid>
Look for:
threadpool-thread-count— approaches the CPU count + injection rate, never servingthreadpool-queue-length— climbs into the hundredselapsed-timeon each request — linearly increasing
The fix
Change the synchronous call to its async equivalent:
[HttpGet("recent")]
public async Task<IActionResult> GetRecentOrders([FromQuery] int count = 50)
{
var orders = await _db.Orders
.OrderByDescending(o => o.CreatedAt)
.Take(count)
.ToListAsync(); // ASYNC — does not block the thread
return Ok(orders);
}
The thread returns to the pool while the database query is in flight. With 25 concurrent users, the same 8-16 threads can interleave all 25 queries because they yield at the await point instead of blocking.
Deeper checks for production
-
Check every
Task-returning method for sync calls — theasynckeyword on the method signature is irrelevant; the calling pattern inside the method body is what matters. A method markedasync Taskthat callsToList(),Read(),Send()(sync), or.Result/.Wait()will still block. -
ConfigureAwait(false) in library code — if the EF query is inside a library method, ensure you call
.ConfigureAwait(false)to avoid re-entering the request’sSynchronizationContext(though ASP.NET Core does not have one by default, it’s still a good practice for library code). -
Set the minimum thread pool threads — as a safety net, increase
ThreadPool.SetMinThreads(workerThreads, completionPortThreads)to a reasonable baseline (e.g. 100 for a 4-core box) so the pool doesn’t need to ramp up under load. -
Enable Kestrel connection logging — add
app.UseHttpLogging()inProgram.csto see exactly when connections arrive and when they close.
Prevention checklist
- Every EF call in an async method uses the
Asyncsuffix method (ToListAsync,FirstOrDefaultAsync,SaveChangesAsync) - Every
HttpClientcall usesSendAsync, notSendorPost - Every I/O operation (file, network, database) uses the async API
- Thread pool min threads are set at application startup to avoid ramp-up delay
- Load test (
hey -n 1000 -c 50 http://localhost:5000/api/orders/recent) passes without 502s
Source
dotnet/runtime— ThreadPool source: github.com/dotnet/runtimedotnet/aspnetcore— Kestrel timeout configuration: github.com/dotnet/aspnetcore

Comments