Skip to content

Commit 92fa9ff

Browse files
Merge branch 'vlad/lockprof-fix-threading-mod-cloning-bug' into vlad/lock-remove-is-internal-concept
2 parents c145e41 + 63032bd commit 92fa9ff

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

ddtrace/profiling/collector/_lock.pyx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ class _ProfiledLock:
143143
except (AttributeError, TypeError):
144144
pass
145145

146+
# gevent compatibility — gevent's _patch_existing_locks calls
147+
# type(threading.RLock()) to determine the RLock type, then scans gc.get_objects()
148+
# for instances of that type. When our wrapper is on threading.RLock, the type is
149+
# _ProfiledLock, so ALL profiled lock instances match. gevent then checks each for
150+
# _owner/_RLock__owner; without this property, non-RLock wrappers (e.g. Lock) fail
151+
# the check, causing AssertionError("Found unknown lock implementation").
152+
@property
153+
def _owner(self) -> int | None:
154+
return getattr(self.__wrapped__, "_owner", None)
155+
156+
@_owner.setter
157+
def _owner(self, value: int) -> None:
158+
try:
159+
self.__wrapped__._owner = value
160+
except (AttributeError, TypeError):
161+
pass
162+
146163
# DUNDER methods
147164

148165
def __eq__(self, other: object) -> bool:

tests/profiling/collector/test_threading.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def test_lock_patching_survives_module_reimport():
208208
import sys
209209
import threading
210210

211-
from ddtrace.profiling.collector._lock import LockAllocatorWrapper
211+
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper as LockAllocatorWrapper
212212
from ddtrace.profiling.collector._lock import _ProfiledLock
213213
from ddtrace.profiling.collector.threading import ThreadingLockCollector
214214

@@ -245,7 +245,7 @@ def test_lock_unpatch_after_module_reimport():
245245
import sys
246246
import threading
247247

248-
from ddtrace.profiling.collector._lock import LockAllocatorWrapper
248+
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper as LockAllocatorWrapper
249249
from ddtrace.profiling.collector.threading import ThreadingLockCollector
250250

251251
collector = ThreadingLockCollector()

0 commit comments

Comments
 (0)