Skip to content

Commit b1c180c

Browse files
Fix silent suppression of AssertionError exceptions in _ProfiledLock::_acquire
1 parent d5872b9 commit b1c180c

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

ddtrace/profiling/collector/_lock.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ def _acquire(self, inner_func: Callable[..., Any], *args: Any, **kwargs: Any) ->
127127
for ddframe in frames:
128128
handle.push_frame(ddframe.function_name, ddframe.file_name, 0, ddframe.lineno)
129129
handle.flush_sample()
130-
except Exception:
131-
pass # nosec
130+
except Exception as e:
131+
# _maybe_update_self_name throws AssertionError exceptions which need to propagate
132+
if type(e) is AssertionError:
133+
raise e
132134

133135
def acquire(self, *args: Any, **kwargs: Any) -> Any:
134136
return self._acquire(self.__wrapped__.acquire, *args, **kwargs)

tests/profiling_v2/collector/test_threading.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,36 @@ def test_upload_resets_profile(self) -> None:
979979
with pytest.raises(AssertionError):
980980
pprof_utils.parse_newest_profile(self.output_filename)
981981

982+
def test_assertion_error_suppressed_in_acquire(self) -> None:
983+
""" Ensure that AssertionError exceptions is propagated in the `_acquire` method. """
984+
985+
with self.collector_class(capture_pct=100):
986+
lock = self.lock_class()
987+
988+
# Patch _maybe_update_self_name to raise AssertionError
989+
lock._maybe_update_self_name = mock.Mock(
990+
side_effect=AssertionError("test: unexpected frame in stack")
991+
)
992+
993+
with pytest.raises(AssertionError):
994+
# acquire() will propagate AssertionError if _maybe_update_self_name raises it
995+
lock.acquire()
996+
997+
def test_other_exceptions_are_suppressed(self) -> None:
998+
""" Ensure that non - AssertionError exceptions are silently suppressed in the `_acquire` method. """
999+
1000+
with self.collector_class(capture_pct=100):
1001+
lock = self.lock_class()
1002+
1003+
# Patch _maybe_update_self_name to raise RuntimeError
1004+
lock._maybe_update_self_name = mock.Mock(
1005+
side_effect=RuntimeError("Some profiling error")
1006+
)
1007+
1008+
# RuntimeError should be caught and suppressed - lock operations should succeed
1009+
lock.acquire()
1010+
lock.release()
1011+
9821012

9831013
class TestThreadingLockCollector(BaseThreadingLockCollectorTest):
9841014
"""Test Lock profiling"""

0 commit comments

Comments
 (0)