Revisiting ASP.NET Session State Locking
I was recently working on classic WebForm asp.net file upload task for one of my customers. He wanted to show the actual progress of the file being uploaded. For that, I used Session variable to keep track of every bit saved to disk and how much is left that keeps getting updated from the page doing the upload task. To show continuous progress to the user, I created one more web method to display the progress by doing a look up on Session variable.
Interestingly, I started seeing strange issues. Access to web method from my jQuery plugin was blocked by asp.net till the time file upload was happening, defeating the purpose of displaying progress. All web method calls were queued and got released only when file upload happened successfully and finally displayed progress to the user after file upload was done. Almost tried everything but no success 🙁
After lot of debugging found out that Session locks were preventing concurrent access to two different calls. By default, all pages have write access to Session. It allows page to hold reader/writer lock on same session for the duration of the page request. As a result, all other calls will go sequential until the previous request finishes.
By doing EnableSessionState to Readonly on required page(s), only writer lock is acquired by page(s) but concurrent reads from other pages/requests can happen, and that solved my problem!
I know it’s a pretty old topic to discuss but thought of sharing this with community. It might save some of your time 🙂
More information on session state implementation in asp.net: http://msdn.microsoft.com/en-us/library/aa479041.aspx