'WSGIRequest' object has no attribute 'session' #1

Open
bkonkle opened this Issue Oct 25, 2010 · 5 comments

Comments

Projects
None yet
4 participants
@bkonkle
Collaborator

bkonkle commented Oct 25, 2010

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.

@bkonkle

This comment has been minimized.

Show comment
Hide comment
@bkonkle

bkonkle Oct 25, 2010

Collaborator

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.

Collaborator

bkonkle commented Oct 25, 2010

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.

@mkai

This comment has been minimized.

Show comment
Hide comment
@mkai

mkai Sep 27, 2011

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.

mkai commented Sep 27, 2011

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.

@umbrae

This comment has been minimized.

Show comment
Hide comment
@umbrae

umbrae Jun 13, 2012

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.

umbrae commented Jun 13, 2012

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.

@bkonkle bkonkle reopened this Jun 13, 2012

@umbrae

This comment has been minimized.

Show comment
Hide comment
@umbrae

umbrae Jun 13, 2012

Thanks. Switching to the Cookie middleware also solved it for us, and is serviceable for our needs. Thanks for providing this library.

umbrae commented Jun 13, 2012

Thanks. Switching to the Cookie middleware also solved it for us, and is serviceable for our needs. Thanks for providing this library.

@declanshanaghy

This comment has been minimized.

Show comment
Hide comment
@declanshanaghy

declanshanaghy Aug 2, 2013

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',
...

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',
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment