Bug Description
ThreadingMock._increment_mock_call() is not thread-safe.
Multiple threads calling the mock simultaneously lose increments
due to race conditions on call_count and other attributes.
Reproducer
import threading
import unittest.mock
m = unittest.mock.ThreadingMock()
LOOPS = 10000
THREADS = 50
def test_function():
for _ in range(LOOPS):
m()
threads = [threading.Thread(target=test_function) for _ in range(THREADS)]
for t in threads:
t.start()
for t in threads:
t.join()
assert m.call_count == LOOPS * THREADS # FAILS intermittently!
Expected vs Actual
- Expected: 500000
- Got: ~460000 (missing calls lost to race condition)
Environment
Linked PRs
Bug Description
ThreadingMock._increment_mock_call() is not thread-safe.
Multiple threads calling the mock simultaneously lose increments
due to race conditions on call_count and other attributes.
Reproducer
Expected vs Actual
Environment
Linked PRs