Skip to content

Commit 4463ee4

Browse files
fix(profiling): stop collector if not running in Python thread (#2894) (#2904)
When forking a worker process, gevent creates native threads, which appear as DummyThread instances in Python. By detecting these cases we prevent running code in child processes that should not run. Co-authored-by: Julien Danjou <[email protected]> (cherry picked from commit e1ab88c) Co-authored-by: Gabriele N. Tornetta <[email protected]>
1 parent 268af9a commit 4463ee4

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

ddtrace/internal/periodic.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ def stop(self):
5656
def run(self):
5757
"""Run the target function periodically."""
5858
while not self.quit.wait(self.interval):
59+
# DEV: Some frameworks, like e.g. gevent, seem to resuscitate some
60+
# of the threads that were running prior to the fork of the worker
61+
# processes. These threads are normally created via the native API
62+
# and are exposed to the child process as _DummyThreads. We check
63+
# whether the current thread is no longer an instance of the
64+
# original thread class to prevent it from running in the child
65+
# process while the state copied over from the parent is being
66+
# cleaned up. The restarting of the thread is responsibility to the
67+
# registered forksafe hooks.
68+
if not isinstance(threading.current_thread(), self.__class__):
69+
break
5970
self._target()
6071
if self._on_shutdown is not None:
6172
self._on_shutdown()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed an issue with gevent worker processes that caused them to crash and
5+
stop.

0 commit comments

Comments
 (0)