Beware of static session

   edit
Follow


The bug: Weird behaviours regarding session management

  The blame: (within session access wrapper static class):

private static IDictionary session;
...
if (session == null)   session = new SessionAdapter(HttpContext.Current.Session);
...

so the first time the last line is called, the then-current-session is stored in a static variable, thus the first session is always referenced even for newer sessions.

 

The reason for the above code being present in the first place is that:

public static void SetSessionTo(IDictionary newSession)
{
    session = newSession;
}

So that tests that a stubbed ‘session’ could have been injected into the session-wrapper-static-class thingie.

Clearly, the implementation was wrong.

  The fix: now the code looks like this:

private static IDictionary stubbedSession;

static IDictionary Session
{
    get { return stubbedSession ?? MonoRailHttpHandler.CurrentContext.Session; }
}

And these were 60 seconds on careless coding.

 

Ken, the careless coder


     Tweet Follow @kenegozi