Difficult issue with django sessions: sometimes session information is erased

I have a strange error with django sessions in my application: several times (about 10 times for ~ 20,000 per day) the user session information is deleted. I followed it through the log files: on page A there is information for the user's session, after which he submits the form, and on the next page his session is empty. I tried two types of storage: memcached + db and db, and this problem is for both of them. I tried to reproduce these scenarios, but everything works as expected, as I said, this is very rare. I also checked that this problem exists for different users, and for them it does not play every time. I have no idea how to catch the root cause, and I don't know what else stands here as a description. If anyone has any ideas, please let me know. If this is important, I run my application using django 1.2 + FastCGI. Thank you

UPD: I checked and see that the session key from use does not change during two consecutive requests, the first request has the actual state of the session, and in the second session variables are associated with an empty one.

+6
source share
2 answers

As a way to debug this problem, I would subclass the standard Django middleware (or what you are currently using):

django.contrib.sessions.middleware.SessionMiddleware

and wrap process_request and (perhaps more importantly) process_response in some additional logs. Then set the middleware middleware to MIDDLEWARE_CLASSES , not the Django file.

You can also confirm that session.save() actually made its changes by trying to read it. Perhaps the problem is the serialization of the session state, and it does not work on a specific key or value that you are trying to save.

None of this will fix your problem, but may help you establish what is happening.

+4
source

As @Steve Mayne noted, it would be nice to make some entries in the method for storing the middleware of sessions and sessions. This is where I start.

In addition, I would like to say that this may be a database problem, especially if you use the MySQL database backend for sessions. You can check the log for database locks and other concurrency issues. I had to face similar problems before, and the solution is clear: optimization and additional performance.

If you have specific middleware for the application, you can check the functionality that interferes with Django sessions. Such concurrent operations can cause problems if they are not performed properly.

Another thing I would like to do is upgrade to the latest stable version of Django and go on to configure mod_wsgi.

+4
source

All Articles