From afa02732f672af01ef33dbaa1520beabdf577256 Mon Sep 17 00:00:00 2001 From: Nicolas Dietrich Date: Thu, 7 Feb 2013 23:23:53 +0100 Subject: [PATCH] Fix DetachedInstanceError after StatusCodeRedirect In certain cases, e.g. after an exception has been thrown, the database session may be detached, but database objects managed by the orm are still refered to from the same thread. Such objects can be merged with the new db session, as described in http://stackoverflow.com/a/9705458/201743 This is done with `c.user` which is re-retrieved from repoze.who after a StatusCodeRedirect. This fixes what ade22d799ebe894b1f7a9fbc3a5685e6aceca4e7 works around. See also discussion on: https://bitbucket.org/phihag/hhu- adhocracy/issue/168/error-handler-causes-is-not-bound-to-a --- adhocracy/lib/base.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/adhocracy/lib/base.py b/adhocracy/lib/base.py index 8bdade954..b776c4af6 100644 --- a/adhocracy/lib/base.py +++ b/adhocracy/lib/base.py @@ -32,11 +32,12 @@ def __call__(self, environ, start_response): else: c.active_global_nav = 'home' c.user = environ.get('repoze.who.identity', {}).get('user') - try: - if c.user and (c.user.banned or c.user.delete_time): - c.user = None - except DetachedInstanceError, e: - log.exception(e) + + # make sure we're not using a detached user object + c.user = model.meta.Session.merge(c.user) + + if c.user and (c.user.banned or c.user.delete_time): + c.user = None c.active_controller = request.environ.get('pylons.routes_dict')\ .get('controller') c.debug = asbool(config.get('debug'))