diff --git a/profiler.py b/profiler.py index 3b7cd78..07cf2cf 100644 --- a/profiler.py +++ b/profiler.py @@ -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: