Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

KeyError with sessions #131

Closed
jorilallo opened this Issue Nov 24, 2011 · 28 comments

Comments

Projects
None yet
8 participants

I'm seeing these session errors all the time but not for all the users. Happens fro both Facebook and Twitter. A sample:

Traceback (most recent call last):

File "/sites/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, _callback_args, *_callback_kwargs)

File "/sites/.virtualenvs/project/local/lib/python2.7/site-packages/django/views/generic/base.py", line 47, in view
return self.dispatch(request, _args, *_kwargs)

File "/sites/.virtualenvs/project/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in dispatch
return handler(request, _args, *_kwargs)

File "/sites/.virtualenvs/project/src/django-socialregistration/socialregistration/views.py", line 208, in get
client = request.session[self.get_client().get_session_key()]

File "/sites/.virtualenvs/project/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 47, in getitem
return self._session[key]

KeyError: 'socialreg:facebook'

I'm seeing the same, mostly from googlebot visiting facebook connect button.

uggel commented Nov 29, 2011

We've got 3 installations with this problem.

i've solved this by changing the socialregistration/views.py", line 208
to
client = request.session.get(self.get_client().get_session_key(), None)
if not client:
return self.render_to_response({'error': u'session error'})

but I guess it's there for a reason, so this fix is just so I don't get the 500 errors

uggel commented Nov 30, 2011

I'll set a breakpoint on this to see what the key actually is. Maybe tomorrow.

it's looking for the key 'socialreg:facebook' as you can see in the stack trace, but it's not present in the session, maybe the bot doesn't accept cookies and doesn't have a session.

Contributor

aljosa commented Dec 2, 2011

not only googlebot problem, i have the same problem with real users

Contributor

aljosa commented Dec 2, 2011

i think the problem is actually in domain. if your project is available on http://example.com and http://www.example.com and facebook app is setup to use http://www.example.com but process was started from http://example.com session won't be there when user returns and you'll get KeyError.
you can fix this with SESSION_COOKIE_DOMAIN = ".example.com" in settings.py.
clearing django_session table recommended

I'm getting this also for setup page so it's not only bots.

Contributor

aljosa commented Dec 2, 2011

have you tried solution in my previous comment?

vlinhart commented Dec 3, 2011

my webpage is available only as example.com, no www. FB app is setup to example.com.

Contributor

aljosa commented Dec 3, 2011

do you have info about agents/browsers used?
since it's iframe, IE requires P3P headers set and safari has some issues with iframes on first load (this is all based on my experience before last canvas app changes).

vlinhart commented Dec 5, 2011

in my case it's always
'HTTP_FROM': 'googlebot(at)googlebot.com',
'HTTP_USER_AGENT': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',

but there is no iframe, it's normal redirect to the facebook page and another back

rustgia commented Dec 7, 2011

I was getting this problem, when trying to use django-socialregistration and facebook. I was able to remediate this by ensuring the following:

  1. The URL accessed through my browser matched what I had set in the domains in facebook application settings on the facebook site.
  2. Ensuring that the domain name for my site (corresponding to SITE_ID in your settings file), matched the URL you were accessing.

I was running django on my dev server 8000 port. I had to make sure that the domain name for my site (in the database) was set to http://myhost:8000/

uggel commented Dec 8, 2011

Ok, I got past the problem now.

domain.com is set up in the django_site table as site 2.
SITE_ID = 2 is set in settings.py
domain.com is my real domain
domain.com is what is set up at facebook

So, what bugged me turned out to be:
SOCIALREGISTRATION_SESSION_KEY = 'socialreg:'
in settings.py. Once it helped me overcome something. (can't remember what).
Commenting it out solves my problem.

UPDATE:
I am actually not yet finished on this one. I had subsequent problems.
I have added
PREPEND_WWW=True
to my settings.py and made sure that
www.domain.com is set up at facebook.
www.domain.com is set up in the django_site table as site 2.

diafygi commented Dec 15, 2011

I'm still having problems, here's my traceback. Anyone else find solutions?

Traceback (most recent call last):

 File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 47, in view
   return self.dispatch(request, *args, **kwargs)

 File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 68, in dispatch
   return handler(request, *args, **kwargs)

 File "/home/user/python-manual-installs/src/socialregistration/socialregistration/views.py", line 208, in get
   client = request.session[self.get_client().get_session_key()]

 File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py", line 47, in __getitem__
   return self._session[key]

KeyError: 'socialreg:twitter'

I'm seeing this for many user agents (examples below), which makes me believe that it's not a browser issue. Howver, I haven't been able to reproduce it on my end. Suggestions for reproducing?

'HTTP_USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)',
'HTTP_USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2',
Contributor

jbergantine commented Dec 19, 2011

I'm getting this as well, similar traceback:

Traceback (most recent call last):

 File "/home/getlostmt/webapps/django/lib/python2.6/Django-1.3-py2.6.egg/django/core/handlers/base.py", line 111, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/home/getlostmt/webapps/django/lib/python2.6/Django-1.3-py2.6.egg/django/views/generic/base.py", line 47, in view
   return self.dispatch(request, *args, **kwargs)

 File "/home/getlostmt/webapps/django/lib/python2.6/Django-1.3-py2.6.egg/django/views/generic/base.py", line 68, in dispatch
   return handler(request, *args, **kwargs)

 File "/home/getlostmt/webapps/django/myproject/socialregistration/views.py", line 208, in get
   client = request.session[self.get_client().get_session_key()]

 File "/home/getlostmt/webapps/django/lib/python2.6/Django-1.3-py2.6.egg/django/contrib/sessions/backends/base.py", line 47, in __getitem__
   return self._session[key]

KeyError: 'socialreg:facebook'

@aljosa All of my requests are using plain domain without www. (configured in nginx, django sites, facebook and twitter). Any idea if the solutions in #132 would help with this?

diafygi commented Dec 19, 2011

Don't think so because for the lines in #132, it's just cleanup so we don't care if they are already missing. Here, I think we need these keys. So for this bug perhaps just a better graceful degradation is needed.

Contributor

aljosa commented Dec 20, 2011

this is really strange, i'm getting errors again but i can't reproduce them.
can anybody reproduce errors?

uggel commented Dec 20, 2011

I got this working fully now with what I wrote above.
Django 1.4 and all other libs fetched from repositories (no pips)

Also, discovered I could not get it to work with Chrome in incognito mode. But that has something to do with session cookies disabled. Right? Didn't bother to find out.

Contributor

aljosa commented Dec 20, 2011

@uggel chrome in incognito mode works, if it's not working for you then it doesn't work.
any reason you're using django 1.4, is there something in it that should fix this issue?

also, i thought before that my solution works but after couple of days it started again.

anybody has any clue why is this happening?

diafygi commented Jan 6, 2012

I've been getting more of these, here's a recent traceback.

Traceback (most recent call last):

 File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/usr/local/lib/python2.7/dist-packages/newrelic-1.0.5.156/newrelic/hooks/framework_django.py", line 427, in __call__
   return self.__wrapped(*args, **kwargs)

 File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 47, in view
   return self.dispatch(request, *args, **kwargs)

 File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 68, in dispatch
   return handler(request, *args, **kwargs)

 File "/home/user/python-manual-installs/src/socialregistration/socialregistration/views.py", line 236, in get
   client = request.session[self.get_client().get_session_key()]

 File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py", line 47, in __getitem__
   return self._session[key]

KeyError: 'socialreg:twitter'
Owner

flashingpumpkin commented Jan 9, 2012

@jorde @aljosa @diafygi @jbergantine @uggel

Can you please provide the session backends that you are using.

As @rustgia pointed out, if domains configured on API providers don't match what you've configured in django.contrib.sites you will get session errors.

Weirdly, I've not seen these errors happening.

diafygi commented Jan 9, 2012

@flashingpumpkin we are just using the default Django session backend (django.contrib.sessions.middleware.SessionMiddleware) with the database backend. Also, we have the same site domain as the app domain (www.fitocracy.com) for both Twitter and Facebook. We are only seeing a small percentage of users with this error (probably a failure rate of 2-5%).

Owner

flashingpumpkin commented Jan 9, 2012

From what I can tell from here there are two reasons for this and #134 happening:

  1. Users / bots access callback pages without going through the whole process for the registration.
  2. The site losing users' sessions while they're going through the auth process on the API provider or getting a new session cookie assigned when coming back to the site.

Number 1 can be fixed by just returning a page with an error message. Unfortunately, this will also happen for every user that fails to go through the complete signup process and swallow these errors silently. Not ideal.

Maybe a little over the top, but another way to handle it would be to set a custom cookie and store the user's client in the cache backend identified by that cookie. This has the benefit of automatically expiring API clients at the cost of more code.

Thoughts?

Contributor

jbergantine commented Jan 10, 2012

@flashingpumpkin. django contrib sessions. My issue may have been cause with the www subdomain. My tests are passing now that I'm forcing www to redirect over to just the domain. I'm assuming subdomains will cause session errors then if that truly stops the session key from not existing?

On Jan 9, 2012, at 2:57 PM, Alen Mujezinovic wrote:

@jorde @aljosa @diafygi @jbergantine @uggel

Can you please provide the session backends that you are using.

As @rustgia pointed out, if domains configured on API providers don't match what you've configured in django.contrib.sites you will get session errors.

Weirdly, I've not seen these errors happening.


Reply to this email directly or view it on GitHub:
#131 (comment)


Joe Bergantine

http://joebergantine.com
+1 406 581 5492

Owner

flashingpumpkin commented Jan 12, 2012

Anyhow, I'll catch the error and display a message to users what went wrong so they can restart the process. Same for #134

Same as @diafygi here but higher error rate. I'm not sing subdomains anywhere

@jorilallo jorilallo closed this Jan 12, 2012

@jorilallo jorilallo reopened this Jan 12, 2012

@pombredanne pombredanne pushed a commit to pombredanne/django-socialregistration that referenced this issue Nov 2, 2013

@jbergantine jbergantine Related to flashingpumpkin#131. I'm suggesting this change without kn…
…owing if this is the proper response (word of warning). I'm getting this error though:



 File "/home/getlostmt/.virtualenvs/getlost/myproject/socialregistration/views.py", line 238, in get
   client = request.session[self.get_client().get_session_key()]

 File "/home/getlostmt/.virtualenvs/getlost/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 47, in __getitem__
   return self._session[key]

KeyError: 'socialreg:facebook'

So I figure wrapping it as it was modified in flashingpumpkin@7cbc936 might be a good way to go?
9265651
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment