Skip to content

gh-142651: use NonCallableMock._lock for thread safety of call_count - #142922

Merged
kumaraditya303 merged 2 commits into
python:mainfrom
kumaraditya303:mock
Mar 10, 2026
Merged

gh-142651: use NonCallableMock._lock for thread safety of call_count#142922
kumaraditya303 merged 2 commits into
python:mainfrom
kumaraditya303:mock

Conversation

@kumaraditya303

@kumaraditya303kumaraditya303 commented Dec 18, 2025

Copy link
Copy Markdown
Contributor

Skipping news as the there is already a news entry in the original PR.

@kumaraditya303

Copy link
Copy Markdown
ContributorAuthor

No failure in 300+ iterations:

0:07:48 load avg: 3.39 [356] test_unittest.testmock.testthreadingmock passed (51.5 sec) -- running (3): test_unittest.testmock.testthreadingmock (51.8 sec), test_unittest.testmock.testthreadingmock (51.7 sec), test_unittest.testmock.testthreadingmock (51.9 sec)0:07:48 load avg: 3.39 [357] test_unittest.testmock.testthreadingmock passed (51.7 sec) -- running (3): test_unittest.testmock.testthreadingmock (51.8 sec), test_unittest.testmock.testthreadingmock (51.7 sec), test_unittest.testmock.testthreadingmock (51.9 sec)0:07:48 load avg: 3.39 [358] test_unittest.testmock.testthreadingmock passed (51.8 sec) -- running (1): test_unittest.testmock.testthreadingmock (51.9 sec)0:07:48 load avg: 3.39 [359] test_unittest.testmock.testthreadingmock passed (51.7 sec)0:07:48 load avg: 3.39 [360] test_unittest.testmock.testthreadingmock passed (51.9 sec)

@chaope

chaope commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

Hi @kumaraditya303 ,

I have two questions:

  • why is NonCallableMock._lock reused here instead of a dedicated threading.Lock object for this part?
  • Could we change call_count to a property that calculates len(call_args_list) when call_count is called? In this way, we can avoid a lock to improve performance.

@colesburycolesbury left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I think changing call_count to a property as @chaope suggests would also work, but this change seems like it has less backwards compatibility risk to me.

@kumaraditya303
kumaraditya303 enabled auto-merge (squash) March 10, 2026 11:43
@kumaraditya303
kumaraditya303 merged commit 728e4a0 into python:mainMar 10, 2026
45 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @kumaraditya303 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 10, 2026
…ll_count` (pythonGH-142922)
(cherry picked from commit 728e4a0)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 10, 2026
…ll_count` (pythonGH-142922)
(cherry picked from commit 728e4a0)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
@bedevere-app

Copy link
Copy Markdown

GH-145739 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label Mar 10, 2026
@bedevere-app

Copy link
Copy Markdown

GH-145740 is a backport of this pull request to the 3.13 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.13 bugs and security fixes label Mar 10, 2026
@kumaraditya303
kumaraditya303 deleted the mock branch March 10, 2026 12:13
kumaraditya303 added a commit that referenced this pull request Mar 10, 2026
…all_count` (GH-142922) (#145740)
gh-142651: use `NonCallableMock._lock` for thread safety of `call_count` (GH-142922)
(cherry picked from commit 728e4a0)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
kumaraditya303 added a commit that referenced this pull request Mar 10, 2026
…all_count` (GH-142922) (#145739)
gh-142651: use `NonCallableMock._lock` for thread safety of `call_count` (GH-142922)
(cherry picked from commit 728e4a0)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@kumaraditya303@chaope@colesbury