Search code examples
google-app-enginesessionwebapp2google-app-engine-python

Stale session stores with webapp2 in GAE for Python


I have a GAE Python app that stores session data in a SessionStore object obtained via webapp2_extras.sessions.get_store(request=self.request).

I have seen sessions persist for days when I don't shut my browser down, but it occurs to me that as admin for the app, I have no way of clearing stale SessionStore objects in the server, that is, when a user's data that will never be accessed again after his or her browser is closed.

Is this a memory leak in my app running on GAE, or does GAE or WEBAPP2 have some strategy for recognizing a stale session and releasing that memory? I can't find an answer in the GAE/WEBAPP2 docs, so if you have a link that provides the answer, that would be appreciated.


Solution

  • You can configure the expiration time of a session store with webapp2_extras.sessions.default_config:

    session_max_age: Default session expiration time in seconds. Limits the duration of the contents of a cookie, even if a session cookie exists. If None, the contents lasts as long as the cookie is valid. Default is None.

    Now if you want to handle the expiration yourself, a strategy for recognizing a stale session might be to store a timestamp in the cookie or the session data and to check it on each page request. When you want to expire the session, call self.session.clear().