Ivan _
Ivan _

Reputation: 1

Why all my requests are blocked until async/await method executed

I have an asp.net mvc async method waiting for 10 second. The problem is IIS Express (production IIS also) stops processing incoming requests until that async method finishes. I thought await keyword frees current thread for new incoming request. But it seems that I missed something.

public async Task<ActionResult> AsyncMethod()
{
    using (DeliveryPortalEntities context = new DeliveryPortalEntities())
    {
        await context.Database.ExecuteSqlCommandAsync("WAITFOR DELAY '00:00:10'");
    }
    return Json(new { status = "ok" });
}

Update! Can anyone explain why during this code execution IIS stops answer requests immediately.

public async Task<ActionResult> AsyncMethod()
{
    await Task.Delay(10000).ConfigureAwait(false);
    return new EmptyResult();
}

Upvotes: 0

Views: 749

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 457402

I thought await keyword frees current thread for new incoming request.

It does; the thread is free. However, the request is not complete. And if that request has a read/write lock on the session, then no other requests for that session can be processed until that lock is free.

To resolve this, you'll need to change your session usage. If you can go sessionless, that's best - it allows really good horizontal scalability. If not, try to go read-only. Long requests with a read-only session lock can run concurrently with other requests in that same session if they also only need a read-only session lock.

Upvotes: 1

Related Questions