From ba585a2c6dbbbc918f5ceffd54eb31212bca85ef Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Fri, 14 Jan 2011 23:18:21 +0000 Subject: [PATCH] Fixed #11193 -- WSGI handler not properly handling lock on error in load_middleware. Thanks to Phillip Sitbon. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15205 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/handlers/wsgi.py | 14 ++++++++---- tests/regressiontests/handlers/__init__.py | 0 tests/regressiontests/handlers/models.py | 0 tests/regressiontests/handlers/tests.py | 25 ++++++++++++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/regressiontests/handlers/__init__.py create mode 100644 tests/regressiontests/handlers/models.py create mode 100644 tests/regressiontests/handlers/tests.py diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index d3632c77ed..2fd99921b8 100644 --- a/django/core/handlers/wsgi.py +++ b/django/core/handlers/wsgi.py @@ -242,10 +242,16 @@ class WSGIHandler(base.BaseHandler): # settings weren't available. if self._request_middleware is None: self.initLock.acquire() - # Check that middleware is still uninitialised. - if self._request_middleware is None: - self.load_middleware() - self.initLock.release() + try: + # Check that middleware is still uninitialised. + if self._request_middleware is None: + self.load_middleware() + except: + # Unload whatever middleware we got + self._request_middleware = None + raise + finally: + self.initLock.release() set_script_prefix(base.get_script_name(environ)) signals.request_started.send(sender=self.__class__) diff --git a/tests/regressiontests/handlers/__init__.py b/tests/regressiontests/handlers/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/handlers/models.py b/tests/regressiontests/handlers/models.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/handlers/tests.py b/tests/regressiontests/handlers/tests.py new file mode 100644 index 0000000000..5e84f71177 --- /dev/null +++ b/tests/regressiontests/handlers/tests.py @@ -0,0 +1,25 @@ +from django.utils import unittest +from django.conf import settings +from django.core.handlers.wsgi import WSGIHandler + +class HandlerTests(unittest.TestCase): + + def test_lock_safety(self): + """ + Tests for bug #11193 (errors inside middleware shouldn't leave + the initLock locked). + """ + # Mangle settings so the handler will fail + old_middleware_classes = settings.MIDDLEWARE_CLASSES + settings.MIDDLEWARE_CLASSES = 42 + # Try running the handler, it will fail in load_middleware + handler = WSGIHandler() + self.assertEqual(handler.initLock.locked(), False) + try: + handler(None, None) + except: + pass + self.assertEqual(handler.initLock.locked(), False) + # Reset settings + settings.MIDDLEWARE_CLASSES = old_middleware_classes +