Skip to content

Commit c07d9ba

Browse files
saisneha196miss-islington
authored andcommitted
gh-150175: Fix ThreadingMock call_count race condition (GH-150176)
ThreadingMock._increment_mock_call() was not thread-safe. Multiple threads calling the mock simultaneously could lose increments due to race conditions on call_count and other attributes. Fix by overriding _increment_mock_call in ThreadingMixin and wrapping it with the existing _mock_calls_events_lock. (cherry picked from commit 388e023) Co-authored-by: saisneha196 <156835592+saisneha196@users.noreply.github.com>
1 parent 25092e8 commit c07d9ba

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

Lib/unittest/mock.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3121,6 +3121,10 @@ def _mock_call(self, *args, **kwargs):
31213121

31223122
return ret_value
31233123

3124+
def _increment_mock_call(self, /, *args, **kwargs):
3125+
with self._mock_calls_events_lock:
3126+
super()._increment_mock_call(*args, **kwargs)
3127+
31243128
def wait_until_called(self, *, timeout=_timeout_unset):
31253129
"""Wait until the mock object is called.
31263130
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix race condition in :class:`unittest.mock.ThreadingMock` where
2+
concurrent calls could lose increments to ``call_count`` and other
3+
attributes due to a missing lock in ``_increment_mock_call``.

0 commit comments

Comments
 (0)