1

Would changing a session variable (i.e. Session["Progress"]) in code below be safe?

This code is part of code-behind of an ASP.Net page.

When running a loop in parallel, two iterations could run simultaneously and cause issues if the same session variable is changed by both iterations.

   public void LongOperation()
    {
        Parallel.For(0, totalMembers,(i,loopState) =>
        {
            Thread.Sleep(2000);//wait some time to simulate processing
            progress++;
            Session["Progress"] = progress;//IS THIS THREAD-SAFE?
        } 
       ); 
   }
Sunil
  • 20,653
  • 28
  • 112
  • 197
  • 1
    The documentation states that instance methods are not threadsafe. Why are you doing parallell work, have you thought about how it will consume threads used for asp.net requests? http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate(v=vs.110).aspx – sisve Jul 28 '14 at 04:29
  • To ensure it is you can always implement some for of locking – 3dd Jul 28 '14 at 04:40
  • @SimonSvensson, Have a look at this where it seems Session state is thread-safe: http://stackoverflow.com/questions/2715532/synchronizing-access-to-a-member-of-the-asp-net-session – Sunil Jul 28 '14 at 05:01
  • @SimonSvensson, Would parallel looping be not advisable in ASP.Net? I thought, that parallelism will automatically optimize itself based on available CPUs, memory and threads, and so I can use it without much risks. – Sunil Jul 28 '14 at 05:03
  • @Sunil, the session state is thread-safe in the context of "there will be only one http-request at any time writing to the session". There's no thread safety if you start your own threads accessing it. Parallel stuff often uses the ThreadPool for scheduling, the same threads used by asp.net requests, thus reducing the threads available for incoming http requests. Could you tell us more why you feel the need for parallel stuff? What problem are you trying to solve? – sisve Jul 28 '14 at 06:32

1 Answers1

3

This is NOT thread safe, but the session across different open page is safe to change.

So under certain circumstances if you call it is safe.

If you call it only one time from you page, then is safe across different pages. The reason for that is that the session is fully lock the session from the start of page load up to the end.

Some similar answers to prove that.

Web app blocked while processing another web app on sharing same session
Does ASP.NET Web Forms prevent a double click submission?
Trying to make Web Method Asynchronous

If you call it more than one time from the same page, from different threads, then you need synchronization.

Notes

  • If this loop is take long to complete then all users will be wait for that calculations and not only the session user. All users will be lock down by the session lock. If too many users call that then all users together will be wait for long time. So you may need to make this calculations on a scheduler task.
  • If the time to process the data inside the loop is smaller than the time the loop takes to create and synchronize the threads, then the parallel loop is takes more time to complete than the a simple loop. So you need to calculate the speed here for the simple loop and the parallel loop.
Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150