Magnus Rahl
Nov 18, 2010
  5769
(1 votes)

Today’s gotcha: Beware of Response.End()

For various reasons related to a SSO scenario I have a custom handler which the client calls when loading a page. The handler was basically three lines of code. This post is about the third line. Which is no longer in there.

Page loading time: 115 seconds

So the client hits my handler and receives a redirect script after which it nicely requests the page redirected to. Then it yawns for almost two minutes before the page loads. Subsequent requests are nice and quick. What is going on?

The application and page lifecycle

Something every ASP.NET developer should know is the Page lifecycle. But it is also useful to know something about the Application lifecycle.

I could quickly determine that the delay didn’t occur in the Page handler because not even Page_PreInit got hit until just before the request returned. Digging into the application events i started making handlers for nearly every one in my Global.asax.cs file. A lot of breakpoints later it was clear where the application stopped.

Session state locks

The application passed Application_PostMapRequestHandler but was then delayed before entering Application_AcquireRequestState. Then it dawned on me: The session state was locked! ASP.NET locks the session state, basically blocking parallell requests in the same session to prevent race conditions between a client’s requests. A request was apparently not releasing this lock.

The culprit

For some reason I had put a call to the HttpResponse.End() method in my custom handler. I never did it before, I just wanted this handler to return its short script line as quickly as possible. Somewhere I had probably seen the use of Response.End() and just threw it in there. Big mistake. Apparently this prevents the application from releasing the lock on the session state.

Nov 18, 2010

Comments

Please login to comment.
Latest blogs
Multiple Anonymous Carts created from external Head front fetching custom Api

Scenario and Problem Working in a custom headless architecture where a NextJs application hosted in Vercel consumes a custom API built in a...

David Ortiz | Oct 11, 2024

Content Search with Optimizely Graph

Optimizely Graph lets you fetch content and sync data from other Optimizely products. For content search, this lets you create custom search tools...

Dileep D | Oct 9, 2024 | Syndicated blog

Omnichannel Analytics Simplified – Optimizely Acquires Netspring

Recently, the news broke that Optimizely acquired Netspring, a warehouse-native analytics platform. I’ll admit, I hadn’t heard of Netspring before,...

Alex Harris - Perficient | Oct 9, 2024 | Syndicated blog

Problem with language file localization after upgrading to Optimizely CMS 12

Avoid common problems with xml file localization when upgrading from Optimizely CMS 11 to CMS 12.

Tomas Hensrud Gulla | Oct 9, 2024 | Syndicated blog

Optimizely Autocomplete (Statistics)

A user starts typing in the search input, and it returns suggestions for phrases they might be searching for. How to achieve this?

Damian Smutek | Oct 9, 2024 | Syndicated blog

Optimizely Forms: You cannot submit this form because an administrator has turned off data storage.

Do not let this error message scare you, the solution is quite simple!

Tomas Hensrud Gulla | Oct 4, 2024 | Syndicated blog