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 +