From 850afe77ace7cf1aa405c45437c2ea860101db55 Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Fri, 27 Jan 2023 10:56:59 -0500 Subject: [PATCH 1/2] fix(profiling): Always use builtin time.sleep As pointed out in https://github.com/getsentry/sentry-python/issues/1813#issuecomment-1406636598, gevent patches the `time` module and `time.sleep` will only release the GIL if there no other greenlets ready to run. This ensures that we always use the builtin `time.sleep` and not the patched version provided by `gevent`. --- sentry_sdk/profiler.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/profiler.py b/sentry_sdk/profiler.py index 0ce44a031b..dac0a065c1 100644 --- a/sentry_sdk/profiler.py +++ b/sentry_sdk/profiler.py @@ -118,6 +118,13 @@ def is_module_patched(*args, **kwargs): return False +try: + from gevent.monkey import get_original + thread_sleep = get_original("time", "sleep") +except ImportError: + thread_sleep = time.sleep + + _scheduler = None # type: Optional[Scheduler] @@ -658,7 +665,7 @@ def run(self): # not sleep for too long elapsed = time.perf_counter() - last if elapsed < self.interval: - time.sleep(self.interval - elapsed) + thread_sleep(self.interval - elapsed) # after sleeping, make sure to take the current # timestamp so we can use it next iteration @@ -720,7 +727,7 @@ def run(self): # not sleep for too long elapsed = time.perf_counter() - last if elapsed < self.interval: - time.sleep(self.interval - elapsed) + thread_sleep(self.interval - elapsed) # after sleeping, make sure to take the current # timestamp so we can use it next iteration From f5c77bb330ae2b994d72fc6d3af58fd4a609da47 Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Fri, 27 Jan 2023 11:15:40 -0500 Subject: [PATCH 2/2] run black --- sentry_sdk/profiler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry_sdk/profiler.py b/sentry_sdk/profiler.py index dac0a065c1..f0f9eb1d34 100644 --- a/sentry_sdk/profiler.py +++ b/sentry_sdk/profiler.py @@ -120,6 +120,7 @@ def is_module_patched(*args, **kwargs): try: from gevent.monkey import get_original + thread_sleep = get_original("time", "sleep") except ImportError: thread_sleep = time.sleep