Skip to content

Commit 2fea9f4

Browse files
fix(profiling): fix possible deadlock (#4108) (#4116)
It is possible that while the profiling StackCollector is handling the on_span_activate event that a gc occurs which could result in a weakref finalize callback being called which then could activate a new span. This sequence of events results in a deadlock as the lock the StackCollector uses is not reentrant. A regression test would be great but we cannot think of a tractable test case which wouldn't be flaky. (cherry picked from commit b0967c4) Co-authored-by: Kyle Verhoog <[email protected]>
1 parent f93976a commit 2fea9f4

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

ddtrace/internal/nogevent.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def is_module_patched(module):
3737
thread_get_ident = get_original(six.moves._thread.__name__, "get_ident")
3838
Thread = get_original("threading", "Thread")
3939
Lock = get_original("threading", "Lock")
40+
RLock = get_original("threading", "RLock")
4041

4142
is_threading_patched = is_module_patched("threading")
4243

ddtrace/profiling/_threading.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ class _ThreadLink(_thread_link_base):
6969
# Key is a thread_id
7070
# Value is a weakref to an object
7171
_thread_id_to_object = attr.ib(factory=dict, repr=False, init=False, type=typing.Dict[int, _weakref_type])
72-
_lock = attr.ib(factory=nogevent.Lock, repr=False, init=False, type=nogevent.Lock)
72+
# Note that this lock has to be reentrant as spans can be activated unexpectedly in the same thread
73+
# ex. during a gc weakref finalize callback
74+
_lock = attr.ib(factory=nogevent.RLock, repr=False, init=False, type=nogevent.RLock)
7375

7476
def link_object(
7577
self,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
profiling: fix a possible deadlock due to spans being activated unexpectedly.

0 commit comments

Comments
 (0)