Skip to content

Conversation

@kumaraditya303
Copy link
Contributor

@kumaraditya303 kumaraditya303 commented Nov 12, 2025

This fixes the issue outlined in https://gist.github.com/kumaraditya303/05cd1c93577bd7f44f573dad61674842

If the object is currently locked by a two-mutex critical section then locking is skipped. Previously locking was skipped only in case where mutex was the first mutex of the two which is decided by memory address so was nondeterministic.

@colesbury
Copy link
Contributor

The behavior looks correct to me. Would you please add a test for this behavior to _testinternalcapi/test_critical_sections.c

@kumaraditya303
Copy link
Contributor Author

Would you please add a test for this behavior to _testinternalcapi/test_critical_sections.c

I have added tests for reacquisition of both one and two mutex critical sections.

@colesbury
Copy link
Contributor

LGTM:

I think it deserves a NEWS entry

I think we should backport to 3.14: it's low risk and avoids a class of bugs in third party extensions that will be annoying for everyone to deal with. @Yhg1s, what do you think?

@Yhg1s
Copy link
Member

Yhg1s commented Nov 14, 2025

Yeah, I think it makes sense to backport. It doesn't really have downsides and as you say, avoids latent bugs in user code in some situations. I wish we could do more to flag those issues, but that's a task for better tooling (which we're also working on), not something we can improve on here.

@kumaraditya303 kumaraditya303 merged commit f26ed45 into python:main Nov 14, 2025
91 of 93 checks passed
@miss-islington-app
Copy link

Thanks @kumaraditya303 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 14, 2025
…x critical section (pythonGH-141476)

(cherry picked from commit f26ed45)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
@kumaraditya303 kumaraditya303 deleted the critical2 branch November 14, 2025 17:48
@bedevere-app
Copy link

bedevere-app bot commented Nov 14, 2025

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

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Nov 14, 2025
kumaraditya303 added a commit that referenced this pull request Nov 14, 2025
…ex critical section (GH-141476) (#141564)

gh-114203: skip locking if object is already locked by two-mutex critical section (GH-141476)
(cherry picked from commit f26ed45)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants