Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET Session not synchronising between threads on update?
    primarykey
    data
    text
    <p>I've got a controller method that returns some JSON. It's pulling a collection out of session, taking the first item one from the collection and then removing it from the collection. If the count at the start of the method was 1, it is now 0.</p> <p>If I retrieve the collection from session again in the same method to check if the collection count is less one, it is.</p> <p>Simple stuff so far.</p> <p>On a subsequent request the collection is pulled from session again, but instead of having a count of 0 as expect, it has 1. The previously removed item has not been removed! Why is this?</p> <p>Though if enough requests are made, the count does indeed end up as zero. It's as if it takes 30 seconds or so for the session object to synchronise across all threads.</p> <p>Can this really be so? Is MVC4 (could be previous versions too?) have some odd session synchronisation problems?</p> <p>I'm using the default InProc session provider and settings.</p> <p>Here's the log:</p> <pre><code>Thread Level Logger Message 23 DEBUG ProjectMvc.Controllers.RpcController Pre-processing - Tasks.Count: 1 23 DEBUG ProjectMvc.Controllers.RpcController Post processing - SessionID: asaq1v5afu0pwz13at4a24hl 23 DEBUG ProjectMvc.Controllers.RpcController Post processing - Tasks.Count: 0 16 DEBUG ProjectMvc.Controllers.RpcController Pre-processing - Tasks.Count: 1 16 DEBUG ProjectMvc.Controllers.RpcController Post processing - SessionID: asaq1v5afu0pwz13at4a24hl 16 DEBUG ProjectMvc.Controllers.RpcController Post processing - Tasks.Count: 0 23 DEBUG ProjectMvc.Controllers.RpcController Pre-processing - Tasks.Count: 1 23 DEBUG ProjectMvc.Controllers.RpcController Post processing - SessionID: asaq1v5afu0pwz13at4a24hl 23 DEBUG ProjectMvc.Controllers.RpcController Post processing - Tasks.Count: 0 24 DEBUG ProjectMvc.Controllers.RpcController Pre-processing - Tasks.Count: 1 24 DEBUG ProjectMvc.Controllers.RpcController Post processing - SessionID: asaq1v5afu0pwz13at4a24hl 24 DEBUG ProjectMvc.Controllers.RpcController Post processing - Tasks.Count: 0 9 DEBUG ProjectMvc.Controllers.RpcController Pre-processing - Tasks.Count: 1 9 DEBUG ProjectMvc.Controllers.RpcController Post processing - SessionID: asaq1v5afu0pwz13at4a24hl 9 DEBUG ProjectMvc.Controllers.RpcController Post processing - Tasks.Count: 0 14 DEBUG ProjectMvc.Controllers.RpcController Pre-processing - Tasks.Count: 1 14 DEBUG ProjectMvc.Controllers.RpcController Post processing - SessionID: asaq1v5afu0pwz13at4a24hl 14 DEBUG ProjectMvc.Controllers.RpcController Post processing - Tasks.Count: 0 </code></pre> <p>If I can't work out why the collection in session is not being updated for other requests I'll have to work around it and store this stuff in a database.</p> <p>Here's the code:</p> <pre><code> public ActionResult GetCoordinatesTask() { var user = Session["User"] as IUser; if (user == null) { Logger.LogError(this, "No user in session!"); return Json(null, JsonRequestBehavior.AllowGet); } if (user.PhotoGeocodingTasks == null || user.PhotoGeocodingTasks.Count == 0) return Json(null, JsonRequestBehavior.AllowGet); lock (user.PhotoGeocodingTasks) { Logger.LogDebug(this, "Pre-processing - PhotoGeocodingTasks.Count" + user.PhotoGeocodingTasks.Count); var task = user.PhotoGeocodingTasks[0]; if (!user.PhotoGeocodingTasks.Remove(task)) Logger.LogError(this, "Could not remove geocoding task after picking it up!"); var encodedTask = new Dictionary&lt;string, string&gt; { {"photoid", task.PhotoId.ToString(CultureInfo.InvariantCulture)}, {"latlong", task.Latitude + "," + task.Longitide} }; Logger.LogDebug(this, "Post processing - SessionID: " + Session.SessionID); Logger.LogDebug(this, "Post processing - PhotoGeocodingTasks.Count" + user.PhotoGeocodingTasks.Count); return Json(encodedTask, JsonRequestBehavior.AllowGet); } } </code></pre> <p><strong>Edit</strong></p> <p>Here's what I have on the web.config for session:</p> <pre><code>&lt;sessionState mode="InProc" customProvider="DefaultSessionProvider"&gt; &lt;providers&gt; &lt;add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /&gt; &lt;/providers&gt; &lt;/sessionState&gt; </code></pre> <p><strong>Edit 2</strong></p> <p>I also tried storing this task data in the HttpRuntime.Cache as well (keyed against a user id) and got the same result. No updated values for other requests. It's like synchronisation is disabled in my configuraiton.</p> <p>I've tried this code on two computers as well, same result.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload