February 10, 2014

ASP.NET Session and concurrent access

Problem

In one of my projects I found some a strange at first sight issue related to concurrent Session usages. During one long request the other parallel requests were waiting until the previous one is finished.
This issue occurs when user tries to download file from ashx-handler. Handler requires Session to get some user-related configuration which is stored there.

Overview

I've tried to dig deeper and that what I've found.
By default no concurrent access to the asp.net session state is allowed. Requests with same SessionID will be locked exclusively to prevent potential corruption of its state.

What does it mean:

When you have request1 `in progress` and trying to do request2 - Session object will be locked by request1 and our code in request2 will be waiting for request1 completed.

ASP.NET Session is thread safe and synchronization mechanism is based on System.Threading.ReaderWriterLock.

This means that we can do many reads but only one writing at the same time.
ASP.NET Session object is configured for full (read\write) access, by default.

That's why we need to configure Session object as 'read-only' on long-term pages to have non-blocking access to Session object from other pages.

Be aware that even you don't use Session object explicitly you have this issue too.

How to reproduce

To reproduce this issue let's create 2 asp.net pages.

Default page:
<%@ Page Language="C#" %>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
      Response.Write("Hello, SessionId " + Session.SessionID);
  }
</script>

Slow page (contains some long-term execution):
<%@ Page Language="C#" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(10000);
        Response.Write("Hello, SessionId " + Session.SessionID);
    }
</script>

web.config
<configuration>
  <system.web>
    <sessionState mode="InProc"/>
  </system.web>
</configuration>
Try to open Slow page and Default page at the same time.
You may think that Default page will be opened immediately, but actually it will happen only after Slow page loading completed.

Resolving

To resolve this issue we can re-configure ASP.NET Session object access.

Session state is configured by using the sessionState element of the system.web configuration section.

You can also configure session state by using the EnableSessionState value in the @ Page directive (more info).

If you need non-blocking access to read from Session:
<%@ Page Language="C#" EnableSessionState="ReadOnly"%>

If you don't need Session:
<%@ Page Language="C#" EnableSessionState="False"%>

26 comments:

Monty Richards said...

Good post. You can also implement the "IReadOnlySessionState" interface if this is in an ASHX handler (I don't think the @Page directive works).

peterson said...

Your information about asp.net frame works is really interesting and innovative. Also I want you to share latest updates about this frameworks. Can you update it in your website? Thanks for sharing
Dot net training

Unknown said...

The Information which you provided is very much useful for Dot Net Training Learners Thank You for Sharing Valuable Information. I like this blog and this is very informative.


Dot Net Course Chennai

Anonymous said...

This doesn't seem reproducible with VS2012

Bill said...

Great blog Rostyslav Yaremchuk, I was curious if you would be interested in a free shirt from my software developer shirt startup. If you are hit me up, you can check out the developer garb here, System Out Of Memory

Cheers!

Unknown said...

Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
python training in chennai | python training in bangalore

python online training | python training in pune

python training in chennai | python training in bangalore

nilashri said...

Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.

Data Science with Python training in chenni
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore

sai said...

After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
python training in tambaram
python training in annanagar
python training in Bangalore

sunshineprofe said...

If you happen to be interested, feel free to shoot me an e-mail. I look forward to hearing from you! Great blog by the way!
industrial safety courses in chennai

Ishu Sathya said...

Excellent Blog, I have read all your blogs. Thanks for sharing important information. Such a nice post.

Selenium training in Chennai
Selenium Courses in Chennai
best ios training in chennai
Digital Marketing Training in Chennai
JAVA J2EE Training Institutes in Chennai
SEO Training Chennai
Best seo training in chennai

tamilselvan said...

It was worth visiting your blog and I have bookmarked your blog. Hope to visit again
devops online training

aws online training

data science with python online training

data science online training

rpa online training

Unknown said...

The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
Microsoft Azure online training
Selenium online training
Java online training
uipath online training
Python online training

Softgen Infotech said...

Thank you so much for the great and very beneficial stuff that you have shared with the world.

Become an Expert In SAP BASIS Training! The most trusted and trending Programming Language. Learn from experienced Trainers and get the knowledge to crack a coding interview, @Softgen Infotech Located in BTM Layout.

Madhuvarsha said...

Thank you for sharing such a nice and interesting blog with us regarding Java. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle.
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

Janu said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.




Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery







saran said...

"such an exceptionally valuable article. Extremely intriguing to peruse this article.
Digital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery

"

saran said...

"Just the way I have expected. Your website really is interesting
Digital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery"

Rashika said...

AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

lavanya said...

Greetings. I was amazing if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
Java training in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Online Training

Jayalakshmi said...

Thanks for sharing nice information and nice article and very useful information.....
more :
oracle training in chennai

oracle training in tambaram

oracle dba training in chennai

oracle dba training in tambaram

ccna training in chennai

ccna training in tambaram

seo training in chennai

seo training in tambaram

shiny said...

Its very useful for me to know about new things..Keep on blogging..



oracle training in chennai

oracle training in annanagar

oracle dba training in chennai

oracle dba training in annanagar

ccna training in chennai

ccna training in annanagar

seo training in chennai

seo training in annanagar

deiva said...

Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
java training in chennai

java training in omr

aws training in chennai

aws training in omr

python training in chennai

python training in omr

selenium training in chennai

selenium training in omr

Rajendra Cholan said...

Title:
Best Software Training Center in Chennai | Infycle Technologies

Description:
Reach to the best software training center in Chennai, Infycle Technologies to enter into the software industry with well-defined skills. Infycle Technologies is one of the rapidly developing software training cum placement centers in Chennai and is generally known for its significance in providing quality hands-on practical training with 100% guaranteed outcomes! Call 7502633633 to book a free demo and to avail the best offers.
Best training in Chennai

Rajendra Cholan said...

Title:
Grab Big Data Hadoop Training in Chennai | Infycle Technologies

Description:
If Big Data is a job that you're dreaming of, then we, Infycle are with you to make your dream into reality. Infycle Technologies offers the best Big Data Hadoop Training in Chennai, with various levels of highly demanded software courses such as Oracle, Java, Python, Hadoop, AWS, etc., in complete practical training with hands-on coaching by specialized tutors in the field. Along with that, the pre-interviews will be given for the candidates, so that, they can face the interviews with complete knowledge. Call 7502633633 for more info.

best training institute in chennai

Reshma said...

Nice blog! Thanks for sharing this valuable information
Devops Training in Hyderabad
devops training in Gurgaon
DevOps Training in Delhi

Hussey said...

This post is so interactive and informative.keep update more information...
Java Training in Tambaram
java course in tambaram