6

We are running an Owin applications on IIS 8.5 on Win 2012 R2 behind a loadbalancer. One some occations, requests to certain URLs hang indefinitely. If the user selects cancel in the browser, and reloads the page, everything is OK and the server responds quickly.

In IIS manager, we can see the requests hanging: Requests hang in IIS manager The hang seems to occur inside the Owin pipeline. We are running .NET 4.5.2 and the latest Owin packages.

Here's the code for the /api/whoami endpoint:

[Authorize]
public class WhoAmIController : ApiController
{
    [Route("whoami")]
    [HttpGet]
    public IHttpActionResult Whoami()
    {
        return Json(ClaimsPrincipal.Current.Identity.Name);
    }
}

An observation is that requests to this endpoint hangs in the AuthenticateRequest stage in the IIS pipeline, not PreExecuteRequestHandler which is the default IIS stage for the OWIN pipeline. Our only authentication method is cookie authentication, which executes in the AuthenticateRequest stage.

Any ideas?

Edit: adjusted screenshot

Vidar Kongsli
  • 826
  • 2
  • 9
  • 20
  • Are you using async await? – Tomas Jansson Oct 21 '15 at 10:23
  • @TomasJansson Yes, we use async await in the application. Whether it is used on the handling of the specific endpoints in question, I am not sure. The application is part custom code, and part IdentityServer3. – Vidar Kongsli Oct 21 '15 at 10:27
  • Ok, then you might have problem with the synchronization context. When doing an await you're not guaranteed to come back to the "correct" thread if you haven't specifed so. This talk from NNUG last year goes through the details: https://vimeo.com/108094560 – Tomas Jansson Oct 21 '15 at 10:33
  • The part about Synchronization context starts around 26:30, and basically what you need to do is probably to add ConfigureAwait(false). This causing the continuation after an await will continue on the thread pool thread. – Tomas Jansson Oct 21 '15 at 10:35

2 Answers2

1

The problem was that we had code executing on the IIS event PreSendRequestHeaders, which apperently is bad according to http://www.asp.net/aspnet/overview/web-development-best-practices/what-not-to-do-in-aspnet,-and-what-to-do-instead#presend

Our intention was to adjust HTTP headers on the way out on all request. The fix was to move the code to the BeginRequest event.

Vidar Kongsli
  • 826
  • 2
  • 9
  • 20
-1

Have you tried adding .ConfigureAwait(false) to your async calls?

Doing so will make the continuation continue on the "correct" thread.

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
  • Thanks for the input. If there is an async/await, it is in the library code and not something we can get to. I'll update the question with a code sample. – Vidar Kongsli Oct 21 '15 at 10:51
  • @VidarKongsli I think you can configure the code when you call to the library code. It might be that this is not the issue though, but I I have experienced problems like this before (like this week) :) – Tomas Jansson Oct 21 '15 at 11:15
  • Anyway, thanks for the tip, altough that did not turn out to be the problem. We found some other weaknesses, scrutinizing the code for `Task`-related issues. – Vidar Kongsli Oct 30 '15 at 12:54