Permalink
Browse files

Added locking to the WSGI profiler middleware since it uses a lot of …

…local variables
  • Loading branch information...
1 parent 3be8882 commit 5d065fe411f91cb53c4ccce17cb01b5367614cd4 @cdman cdman committed Jul 27, 2012
Showing with 19 additions and 14 deletions.
  1. +19 −14 profiler.py
View
@@ -325,6 +325,7 @@ class ProfilerWSGIMiddleware(object):
def __init__(self, app):
template.register_template_library('gae_mini_profiler.templatetags')
+ self._lock = threading.RLock()
self.app = app
self.app_clean = app
self.prof = None
@@ -337,18 +338,24 @@ def __init__(self, app):
self.end = None
def __call__(self, environ, start_response):
-
- # Start w/ a non-profiled app at the beginning of each request
- self.app = self.app_clean
- self.prof = None
- self.recorder = None
- self.temporary_redirect = False
- self.simple_timing = cookies.get_cookie_value("g-m-p-disabled") == "1"
- requeststore.clear_id()
-
# Never profile calls to the profiler itself to avoid endless recursion.
- if config.should_profile(environ) and not environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/"):
+ if not config.should_profile(environ) or environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/"):
+ result = self.app(environ, start_response)
+ for value in result:
+ yield value
+ return
+ try:
+ self._lock.acquire()
+
+ # Start w/ a non-profiled app at the beginning of each request
+ self.app = self.app_clean
+ self.prof = None
+ self.recorder = None
+ self.temporary_redirect = False
+ self.simple_timing = cookies.get_cookie_value("g-m-p-disabled") == "1"
+ requeststore.clear_id()
+
# Set a random ID for this request so we can look up stats later
request_id = requeststore.generate_id()
@@ -439,10 +446,8 @@ def wrapped_appstats_app(environ, start_response):
self.prof = None
requeststore.clear_id()
- else:
- result = self.app(environ, start_response)
- for value in result:
- yield value
+ finally:
+ self._lock.release()
def add_handler(self):
if self.handler is None:

0 comments on commit 5d065fe

Please sign in to comment.