From e824570a23b3939803cee8a3fd6df0fd5e754de0 Mon Sep 17 00:00:00 2001 From: Elrond Date: Sat, 7 Apr 2012 23:21:59 +0200 Subject: [PATCH] Cleanup sql session after request. ALWAYS! The cleanup could be missed if the request handling code in app.py:__call__ exits early (due to exception, or due to one of those early "return"s). So to make sure the sql session is cleaned up for real, wrap the whole thing in a try: finally:. Also wrote a short tool to test if the session is actually empty. The tool is currently disabled, but ready to be used. --- mediagoblin/app.py | 19 +++++++++++-------- mediagoblin/db/sql/open.py | 6 ++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/mediagoblin/app.py b/mediagoblin/app.py index 0a57c091..b7ca092d 100644 --- a/mediagoblin/app.py +++ b/mediagoblin/app.py @@ -109,7 +109,7 @@ class MediaGoblinApp(object): self.meddleware = [common.import_component(m)(self) for m in meddleware.ENABLED_MEDDLEWARE] - def __call__(self, environ, start_response): + def call_backend(self, environ, start_response): request = Request(environ) ## Routing / controller loading stuff @@ -184,15 +184,18 @@ class MediaGoblinApp(object): for m in self.meddleware[::-1]: m.process_response(request, response) - # Reset the sql session, so that the next request - # gets a fresh session + return response(environ, start_response) + + def __call__(self, environ, start_response): + ## If more errors happen that look like unclean sessions: + # self.db.check_session_clean() + try: + return self.call_backend(environ, start_response) + finally: + # Reset the sql session, so that the next request + # gets a fresh session self.db.reset_after_request() - except TypeError: - # We're still on mongo - pass - - return response(environ, start_response) def paste_app_factory(global_config, **app_config): diff --git a/mediagoblin/db/sql/open.py b/mediagoblin/db/sql/open.py index edbf0785..ce5f0604 100644 --- a/mediagoblin/db/sql/open.py +++ b/mediagoblin/db/sql/open.py @@ -37,6 +37,12 @@ class DatabaseMaster(object): Session.add(obj) Session.flush() + def check_session_clean(self): + for dummy in Session(): + _log.warn("STRANGE: There are elements in the sql session. " + "Please report this and help us track this down.") + break + def reset_after_request(self): Session.rollback() Session.remove() -- 2.25.1