Skip to content

Commit 9d23e5f

Browse files
authored
fix(profiling): Always use builtin time.sleep (#1869)
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 c2ed5ec commit 9d23e5f

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

sentry_sdk/profiler.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,24 @@
109109
)
110110

111111

112-
try:
113-
from gevent.monkey import is_module_patched # type: ignore
114-
except ImportError:
115-
116-
def is_module_patched(*args, **kwargs):
117-
# type: (*Any, **Any) -> bool
118-
# unable to import from gevent means no modules have been patched
119-
return False
120-
121-
122112
try:
123113
from gevent import get_hub as get_gevent_hub # type: ignore
114+
from gevent.monkey import get_original, is_module_patched # type: ignore
115+
116+
thread_sleep = get_original("time", "sleep")
124117
except ImportError:
125118

126119
def get_gevent_hub():
127120
# type: () -> Any
128121
return None
129122

123+
thread_sleep = time.sleep
124+
125+
def is_module_patched(*args, **kwargs):
126+
# type: (*Any, **Any) -> bool
127+
# unable to import from gevent means no modules have been patched
128+
return False
129+
130130

131131
def is_gevent():
132132
# type: () -> bool
@@ -797,7 +797,7 @@ def run(self):
797797
# not sleep for too long
798798
elapsed = time.perf_counter() - last
799799
if elapsed < self.interval:
800-
time.sleep(self.interval - elapsed)
800+
thread_sleep(self.interval - elapsed)
801801

802802
# after sleeping, make sure to take the current
803803
# timestamp so we can use it next iteration
@@ -859,7 +859,7 @@ def run(self):
859859
# not sleep for too long
860860
elapsed = time.perf_counter() - last
861861
if elapsed < self.interval:
862-
time.sleep(self.interval - elapsed)
862+
thread_sleep(self.interval - elapsed)
863863

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

0 commit comments

Comments
 (0)