Skip to content

Commit 819dc57

Browse files
use NonCallableMock._lock for thread safety of call_count
1 parent 8b64dd8 commit 819dc57

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Lib/unittest/mock.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,10 +1184,16 @@ def _increment_mock_call(self, /, *args, **kwargs):
11841184
# handle call_args
11851185
# needs to be set here so assertions on call arguments pass before
11861186
# execution in the case of awaited calls
1187-
_call = _Call((args, kwargs), two=True)
1188-
self.call_args = _call
1189-
self.call_args_list.append(_call)
1190-
self.call_count = len(self.call_args_list)
1187+
with NonCallableMock._lock:
1188+
# Lock is used here so that call_args_list and call_count are
1189+
# set atomically otherwise it is possible that by the time call_count
1190+
# is set another thread may have appended to call_args_list.
1191+
# The rest of this function relies on list.append being atomic and
1192+
# skips locking.
1193+
_call = _Call((args, kwargs), two=True)
1194+
self.call_args = _call
1195+
self.call_args_list.append(_call)
1196+
self.call_count = len(self.call_args_list)
11911197

11921198
# initial stuff for method_calls:
11931199
do_method_calls = self._mock_parent is not None

0 commit comments

Comments
 (0)