joerussbowman / gaeutilities

gaeutilities - A collection of utilities to help with application development on Google Appengine
http://gaeutilities.appspot.com
BSD 3-Clause "New" or "Revised" License
78 stars 4 forks source link

BadKeyError on a not Cookie Session #21

Closed apenasisso closed 14 years ago

apenasisso commented 14 years ago

Hi, some times I'm experiencing this error, this is not running on devserver:

Traceback (most recent call last): File "/base/data/home/apps/tfinteg/live-2.342328411530955095/principal.py", line 70, in get self.sess = sessions.Session() File "/base/data/home/apps/tfinteg/live-2.342328411530955095/appengine_utilities/sessions.py", line 562, in init self.session = _AppEngineUtilities_Session.get_session(self) File "/base/data/home/apps/tfinteg/live-2.342328411530955095/appengine_utilities/sessions.py", line 142, in get_session ds_session = db.get(str(session_key)) File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/db/init.py", line 1243, in get keys, multiple = datastore.NormalizeAndTypeCheckKeys(keys) File "/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore.py", line 150, in NormalizeAndTypeCheckKeys keys = [_GetCompleteKeyOrError(key) for key in keys] File "/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore.py", line 2400, in _GetCompleteKeyOrError key = Key(arg) File "/base/python_runtime/python_lib/versions/1/google/appengine/api/datastore_types.py", line 270, in init 'Invalid string key %s. Details: %s' % (encoded, e)) BadKeyError: Invalid string key agd0ZmludGVnciMLEhtfQXBwRW5naW5lVXRpbGl0aWVzX1Nlc3Npb24Y0===. Details: Incorrect padding

joerussbowman commented 14 years ago

Think there might be a typo in the session code, can you try changing line 142

-ds_session = db.get(str(session_key))

+ds_session = db.get(str(session_key()))

apenasisso commented 14 years ago

Ok, I will change. But I will not be able to test. By now there is no private informations on my session. Well, I just use the token as reference in my User Model. I will change to Cookie Writer I know that the performance will increase, but, the previous error have a chance to happen again?

Thanks

joerussbowman commented 14 years ago

The error should not happen with cookie writer, it's specific to the interaction between the application and the datastore.

apenasisso commented 14 years ago

By some motive now with session_key() and without cookie writer, Python raises a UnicodeError.

joerussbowman commented 14 years ago

you made the suggested change and got UnicodeError?

joerussbowman commented 14 years ago

err yea, you did, sorry still drinking coffee... ok I'd revert the change. I'll have to look into the initial issue. It looks pretty internal to the datastore, which Google has recently said is having growing pains issues. I'm not saying that that's the issue, but it could be. I won't know more until I can get time to look at it, and unfortunately my wife is due any day now, so not sure when I'll have that time.

apenasisso commented 14 years ago

That's ok, I made a poor Workaround. If got an error, renew the session. Thanks

yoosung commented 14 years ago

i have same issue when there is a '_' character in a sid, which may be produced by base64.urlsafe_encode().

so below is my fix:

appengine_utilities/sessions.py, line 116

apenasisso commented 14 years ago

thanks, I will try, hope that works... thanks again, hehe

joerussbowman commented 14 years ago

I've applied the patch to the master branch, thanks yoosung

durden commented 13 years ago

What is the correct way to renew a session? I'm seeing this error if I'm developing locally, have session in the browser, and then switch to running on my deployed instance. It looks like calling Session() tries to connect to the session id stored in my browser, which is only saved in the local datastore, not deployed one.

I'd like to detect this and just create a new session in the deployed/live datastore. I don't see a clear way to say, 'oops session from browser wasn't found in datastore, so start a new one.'

joerussbowman commented 13 years ago

Well, session is a dict so you could try

if not "key" in session: session = Session() session["key"] = "val"

durden commented 13 years ago

Ok, thanks. I didn't look around enough and realized that my problem is lessened by the addition of the try/catch around db.get(key). I noticed this was in the master branch, and not in the release. Thanks, this library has been a big help!