Randy Tang
Randy Tang

Reputation: 4353

gaesession on Google App Engine with Python: KeyError?

I used gaesessions on Google App Engine with Python to handle user login sessions. The following are some of my codes:

When a user logs in:

from gaesessions import get_current_session
...

session = get_current_session()
if session.is_active():
    session.terminate()
session['account'] = account
# Do some stuff to log the user in

When the user logs out:

session = get_current_session()
account = session['account']
# Do some stuff to log the user out

The above codes worked just fine most of the time, except that sometimes (very rarely, maybe once in a month) GAE complained about the statement account = session['account'] when the user logs out with the error message: KeyError: 'account' I wonder if anybody has encountered the same problem?

BTW, I also clean up expired sessions as below. Is it necessary? (I have no idea when a session goes expired) Or could it be the cause of the problem? Thanks.

while not delete_expired_sessions():
    pass

Upvotes: 0

Views: 486

Answers (1)

Albert
Albert

Reputation: 3651

Your session dictionary doesn't have the account key set to anything. This could be that a request runs your logout method without being logged in. This could happen by someone who is not logged in, yet visits your logout handler.

Eg. A user requests yourwebsite.com/logout without being actually logged in yet.

Upvotes: 1

Related Questions