Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net weird session bug - variables disappearing due to new session during postback

I've got a really, really strange problem which I'm hoping someone can shed some light on. We have a form which has been causing us problems, and through debugging etc. realised it was due to a session variable that was 'randomly' disappearing.

We know it's not code doing it - so to eliminate this we knocked up a really simple form with a literal and a button. During page load, if it's not a postback it sets a session variable, say "myvalue" with the date and time. When the user clicks the button, it checks the session for that variable and then displays whether it was there or not. It will then abandon the session so that next runs will be cleared (it doesn't do this in the form I described above - this is just for testing).

However, 90 times out of a 100 (for arguments sake), it will act as you'd expect. However for those 10, spread out across the test randomly - so no specific time lapses or test repeats - it will report that the variable has gone.

Now I've confirmed that this is 100% due to the session restarting itself at completely random intervals. On first page load, it states that IsNewSession is true - which is what I'd expect as it's first time in. On postback, it is set to IsNewSession = false - again what I'd expect, and to boot my variable is in place.

However, when it goes wrong, the initial request IsNewSession is true, but on postback it's set to true again - with no variables in the store.

The environment is using the AspState database (SQL Server). There's nothing obvious going on - and whats worse is the random nature of the problem. Timeouts are virtually eliminated. The only difference is that this is on a LOAD BALANCED environment across 4 servers. We've checked the application ID in IIS (7.5) and they are all the same - same machine keys etc.

Does anyone have any idea what is going on here? It's .Net 2 btw, which may be the problem - I'm hoping it's a known issue with this versions' state management, but as yet I can't find anything that may point the cause.

The problem does not seem to show up when using InProc on one server. It only seems to happen using Sql Server.

Thanks in advance - really appreciate any help,

Cheers, Tony

Update:

We think we've worked out the problem. The stored procedure, DeleteExpiredSessions, runs every minute - which is the adapted version microsoft recommend in their KB article http://support.microsoft.com/kb/973849. This was running on live every minute - so we replicated this in test (which is also load balanced) and set up the job to run on the same interval.

The same happened in test. By disabling this in live, the problem has now disappeared.

It's obviously either locking up (which we cannot see in the logs or activity monitor) or removing sessions that are still valid.

Has anyone ever seen this before? My current theory is that the session is initialised on each call to the server you land on, but then on postback redirection to another server for whatever reason the session is considered 'invalid' and recycled.

like image 545
PoorbandTony Avatar asked Sep 18 '13 15:09

PoorbandTony


1 Answers

Thought I'd post a (long overdue) update - it was definitely the stored procedure causing the problem in the end, as per my update above. If you're having the same problems I suggest completely disabling it to verify - but it certainly did the trick for us.

like image 118
PoorbandTony Avatar answered Oct 24 '22 05:10

PoorbandTony