Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ThreadingMock.call_count is not thread-safe (race condition) #150175

Description

@saisneha196

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions