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
This commit is contained in:
Andrew Godwin 2011-01-14 23:18:21 +00:00
parent faa4a98f27
commit ba585a2c6d
4 changed files with 35 additions and 4 deletions

View File

@ -242,9 +242,15 @@ class WSGIHandler(base.BaseHandler):
# settings weren't available. # settings weren't available.
if self._request_middleware is None: if self._request_middleware is None:
self.initLock.acquire() self.initLock.acquire()
try:
# Check that middleware is still uninitialised. # Check that middleware is still uninitialised.
if self._request_middleware is None: if self._request_middleware is None:
self.load_middleware() self.load_middleware()
except:
# Unload whatever middleware we got
self._request_middleware = None
raise
finally:
self.initLock.release() self.initLock.release()
set_script_prefix(base.get_script_name(environ)) set_script_prefix(base.get_script_name(environ))

View File

View File

@ -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