https://github.com/python/cpython/commit/8053ead86b8ca5f97472366a3623c53ee3a15350
commit: 8053ead86b8ca5f97472366a3623c53ee3a15350
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2026-05-26T12:46:42-07:00
summary:

[3.14] gh-150175: Fix ThreadingMock call_count race condition (GH-150176) 
(#150181)

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 388e023fe1197c1ffed374520ed45df4ac72b8f5)

Co-authored-by: saisneha196 <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-05-21-11-25-58.gh-issue-150175.8H4Caz.rst
M Lib/unittest/mock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 92b81d1584142eb..5a8fccf59dcb6e4 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -3121,6 +3121,10 @@ def _mock_call(self, *args, **kwargs):
 
         return ret_value
 
+    def _increment_mock_call(self, /, *args, **kwargs):
+        with self._mock_calls_events_lock:
+            super()._increment_mock_call(*args, **kwargs)
+
     def wait_until_called(self, *, timeout=_timeout_unset):
         """Wait until the mock object is called.
 
diff --git 
a/Misc/NEWS.d/next/Library/2026-05-21-11-25-58.gh-issue-150175.8H4Caz.rst 
b/Misc/NEWS.d/next/Library/2026-05-21-11-25-58.gh-issue-150175.8H4Caz.rst
new file mode 100644
index 000000000000000..80fc80d4d50a636
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-05-21-11-25-58.gh-issue-150175.8H4Caz.rst
@@ -0,0 +1,3 @@
+Fix race condition in :class:`unittest.mock.ThreadingMock` where
+concurrent calls could lose increments to ``call_count`` and other
+attributes due to a missing lock in ``_increment_mock_call``.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to