From 0eec302671f0c59b830da8a392cec0e4994c17c3 Mon Sep 17 00:00:00 2001 From: Richard Ebeling Date: Mon, 28 Mar 2022 23:28:14 +0200 Subject: [PATCH] Fix LoggedModel.thread.request not being cleared in failing tests (#1727) --- evap/evaluation/middleware.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/evap/evaluation/middleware.py b/evap/evaluation/middleware.py index 7ebaf85532..ed6ba2466f 100644 --- a/evap/evaluation/middleware.py +++ b/evap/evaluation/middleware.py @@ -19,9 +19,14 @@ def __call__(self, request): LoggedModel.thread.request = request LoggedModel.thread.request_id = str(uuid.uuid4()) - response = self.get_response(request) - - del LoggedModel.thread.request - del LoggedModel.thread.request_id + try: + # django documentation says to just do this without the try, and that exceptions will be handled: + # https://docs.djangoproject.com/en/4.0/topics/http/middleware/#writing-your-own-middleware + # However, django-webtest sets DEBUG_PROPAGATE_EXCEPTIONS, and propagated exceptions caused our deletion to + # be skipped, leading to weird errors in the tests executed afterwards. See #1727. + response = self.get_response(request) + finally: + del LoggedModel.thread.request + del LoggedModel.thread.request_id return response