Skip to content

Commit

Permalink
fix(profiling): Always use builtin time.sleep (#1869)
Browse files Browse the repository at this point in the history
As pointed out in #1813 (comment),
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`.
  • Loading branch information
Zylphrex committed Jan 30, 2023
1 parent c2ed5ec commit 9d23e5f
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9d23e5f

Please sign in to comment.