Skip to content

Commit 850afe7

Browse files
committed
fix(profiling): Always use builtin time.sleep
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`.
1 parent 762557a commit 850afe7

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

sentry_sdk/profiler.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ def is_module_patched(*args, **kwargs):
118118
return False
119119

120120

121+
try:
122+
from gevent.monkey import get_original
123+
thread_sleep = get_original("time", "sleep")
124+
except ImportError:
125+
thread_sleep = time.sleep
126+
127+
121128
_scheduler = None # type: Optional[Scheduler]
122129

123130

@@ -658,7 +665,7 @@ def run(self):
658665
# not sleep for too long
659666
elapsed = time.perf_counter() - last
660667
if elapsed < self.interval:
661-
time.sleep(self.interval - elapsed)
668+
thread_sleep(self.interval - elapsed)
662669

663670
# after sleeping, make sure to take the current
664671
# timestamp so we can use it next iteration
@@ -720,7 +727,7 @@ def run(self):
720727
# not sleep for too long
721728
elapsed = time.perf_counter() - last
722729
if elapsed < self.interval:
723-
time.sleep(self.interval - elapsed)
730+
thread_sleep(self.interval - elapsed)
724731

725732
# after sleeping, make sure to take the current
726733
# timestamp so we can use it next iteration

0 commit comments

Comments
 (0)