From 9d23e5fc08a58da41e9894823236060738889e81 Mon Sep 17 00:00:00 2001 From: Tony Xiao Date: Mon, 30 Jan 2023 10:37:00 -0500 Subject: [PATCH] fix(profiling): Always use builtin time.sleep (#1869) 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 | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/sentry_sdk/profiler.py b/sentry_sdk/profiler.py index 3277cebde4..3306f721f7 100644 --- a/sentry_sdk/profiler.py +++ b/sentry_sdk/profiler.py @@ -109,24 +109,24 @@ ) -try: - from gevent.monkey import is_module_patched # type: ignore -except ImportError: - - def is_module_patched(*args, **kwargs): - # type: (*Any, **Any) -> bool - # unable to import from gevent means no modules have been patched - return False - - try: from gevent import get_hub as get_gevent_hub # type: ignore + from gevent.monkey import get_original, is_module_patched # type: ignore + + thread_sleep = get_original("time", "sleep") except ImportError: def get_gevent_hub(): # type: () -> Any return None + thread_sleep = time.sleep + + def is_module_patched(*args, **kwargs): + # type: (*Any, **Any) -> bool + # unable to import from gevent means no modules have been patched + return False + def is_gevent(): # type: () -> bool @@ -797,7 +797,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 @@ -859,7 +859,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