'WSGIRequest' object has no attribute 'session'
Opened this issue · 5 comments
I can't reproduce this in development, but in production I get this error when using PinningSessionMiddleware: 'WSGIRequest' object has no attribute 'session'
For some reason, the request object passed to process_response
doesn't have the session data. I suspect this may have something to do with the many custom middleware classes we use, but the confusing part is that everything works fine on our dev/testing server. I'm tracking this issue here in case someone else runs into it. If you have this issue, let me know here so I can help track down the root cause.
I believe this is an issue related to my specific production configuration, and not with django-balancer itself. Switching to PinningCookieMiddleware resolved the issue for my app. If anyone runs into this issue out in the wild, please reopen this issue so that I can look into it further.
I had this issue once yesterday shortly after restarting the application server. Could it be that the session data is being deleted or invalidated upon app shutdown? I'm using Django 1.4's signed_cookies session engine as the backend.
For the record, we're having this issue at Readability. It appears to be happening sporadically on things that explicitly have no session to start (Like curl, pingdom hits, etc).
We're going to try the Cookie middleware and see how that fares.
Thanks. Switching to the Cookie middleware also solved it for us, and is serviceable for our needs. Thanks for providing this library.
The reason this happens is because the order of the middleware is important.
During the request the Django session middleware must execute first in order to add the session object to the request.
Ensure that middleware is loaded in this order:
...
# SessionMiddleware must be before PinningSessionMiddleware
'django.contrib.sessions.middleware.SessionMiddleware',
# Pin DB reads to the master DB after write.
'balancer.middleware.PinningSessionMiddleware',
...