Title: `ThreadingMock.call_count` is not thread safe · Issue #142651 · python/cpython · GitHub
Open Graph Title: `ThreadingMock.call_count` is not thread safe · Issue #142651 · python/cpython
X Title: `ThreadingMock.call_count` is not thread safe · Issue #142651 · python/cpython
Description: Bug report Bug description: Running this test program with (plain old, non-free-threaded) Python 3.14 fails: import threading import unittest.mock m = unittest.mock.ThreadingMock() LOOPS = 10_000 THREADS = 10 def test_function(): for _ i...
Open Graph Description: Bug report Bug description: Running this test program with (plain old, non-free-threaded) Python 3.14 fails: import threading import unittest.mock m = unittest.mock.ThreadingMock() LOOPS = 10_000 T...
X Description: Bug report Bug description: Running this test program with (plain old, non-free-threaded) Python 3.14 fails: import threading import unittest.mock m = unittest.mock.ThreadingMock() LOOPS = 10_000 T...
Opengraph URL: https://github.com/python/cpython/issues/142651
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`ThreadingMock.call_count` is not thread safe","articleBody":"# Bug report\n\n### Bug description:\n\nRunning this test program with (plain old, non-free-threaded) Python 3.14 fails:\n```python\nimport threading\nimport unittest.mock\n\nm = unittest.mock.ThreadingMock()\n\nLOOPS = 10_000\nTHREADS = 10\n\n\ndef test_function():\n for _ in range(LOOPS):\n m()\n\n\nthreads = [threading.Thread(target=test_function) for _ in range(THREADS)]\nfor thread in threads:\n thread.start()\nfor thread in threads:\n thread.join()\n\nassert m.call_count == LOOPS * THREADS, f\"Expected {LOOPS * THREADS}, got {m.call_count}\"\n```\n\nResults are something like:\n```sh\n$ python3.14 python_mock_call_count_atomicity.py\nTraceback (most recent call last):\n File \"python_mock_call_count_atomicity.py\", line 21, in \u003cmodule\u003e\n assert m.call_count == LOOPS * THREADS, f\"Expected {LOOPS * THREADS}, got {m.call_count}\"\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nAssertionError: Expected 100000, got 95507\n```\n\nThis is essentially the same bug as #122957, but that was closed with just a fix to the test suite. This is a bug report that the version of MagicMock for multithreading tests is not thread-safe and so the obvious way of writing those multithreading tests may not work.\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-142656\n* gh-142674\n* gh-142743\n* gh-142744\n* gh-142922\n* gh-145739\n* gh-145740\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/godlygeek","@type":"Person","name":"godlygeek"},"datePublished":"2025-12-12T21:46:52.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/142651/cpython/issues/142651"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f14d1c4b-dc9d-7d3e-b7da-a3951d81d63e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DDBA:14F3B3:16C1CC:1E9F68:6A51D2FA |
| html-safe-nonce | bf74de8ce4439ef2c1c8e86d44bf948b741c3d5ceeca1c95fb05b93e7814cf3b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEREJBOjE0RjNCMzoxNkMxQ0M6MUU5RjY4OjZBNTFEMkZBIiwidmlzaXRvcl9pZCI6IjI3NjM0Mzk2NTc0OTQ2NTg1MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c572e5331f4c5b0f47918a694874a68ee042e217a5b8da2baaa8390c13c5569e |
| hovercard-subject-tag | issue:3724925789 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/142651/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5289746af59b9da4963db09b09a3d31a7508026954db146f3bab2de80ecc8294/python/cpython/issues/142651 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5289746af59b9da4963db09b09a3d31a7508026954db146f3bab2de80ecc8294/python/cpython/issues/142651 |
| og:image:alt | Bug report Bug description: Running this test program with (plain old, non-free-threaded) Python 3.14 fails: import threading import unittest.mock m = unittest.mock.ThreadingMock() LOOPS = 10_000 T... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | godlygeek |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width