Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #11193 -- WSGI handler not properly handling lock on error in l…

…oad_middleware. Thanks to Phillip Sitbon.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15205 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ba585a2c6dbbbc918f5ceffd54eb31212bca85ef 1 parent faa4a98
@andrewgodwin andrewgodwin authored
View
14 django/core/handlers/wsgi.py
@@ -242,10 +242,16 @@ def __call__(self, environ, start_response):
# 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__)
View
0  tests/regressiontests/handlers/__init__.py
No changes.
View
0  tests/regressiontests/handlers/models.py
No changes.
View
25 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
+
Please sign in to comment.
Something went wrong with that request. Please try again.