From 6d31e431c3bb09cf386c0fbef169a5307e689775 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 12 Aug 2007 10:24:05 +0000 Subject: [PATCH] Fixed #4909 -- Fixed a race condition with middleware initialisation in multi-threaded setups. Thanks, colin@owlfish.com. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5868 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/core/handlers/wsgi.py | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index d2f303c12b..27eaf5bd1d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -78,6 +78,7 @@ answer newbie questions, and generally made Django that much better: Bryan Chow Michal Chruszcz Ian Clelland + colin@owlfish.com crankycoder@gmail.com Pete Crosier Matt Croydon diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index 2db83fccfc..99d400d1bb 100644 --- a/django/core/handlers/wsgi.py +++ b/django/core/handlers/wsgi.py @@ -6,6 +6,7 @@ from django.utils.encoding import force_unicode from django import http from pprint import pformat from shutil import copyfileobj +from threading import Lock try: from cStringIO import StringIO except ImportError: @@ -176,13 +177,19 @@ class WSGIRequest(http.HttpRequest): raw_post_data = property(_get_raw_post_data) class WSGIHandler(BaseHandler): + initLock = Lock() + def __call__(self, environ, start_response): from django.conf import settings # Set up middleware if needed. We couldn't do this earlier, because # settings weren't available. if self._request_middleware is None: - self.load_middleware() + self.initLock.acquire() + # Check that middleware is still uninitialised. + if self._request_middleware is None: + self.load_middleware() + self.initLock.release() dispatcher.send(signal=signals.request_started) try: