Skip to content

gh-155400: Fix deadlock with type_lock_prevent_release() - #155401

Merged
kumaraditya303 merged 2 commits into
python:mainfrom
nascheme:gh-155400-type-lock-prevent-release-deadlock
Aug 15, 2026
Merged

gh-155400: Fix deadlock with type_lock_prevent_release()#155401
kumaraditya303 merged 2 commits into
python:mainfrom
nascheme:gh-155400-type-lock-prevent-release-deadlock

Conversation

@nascheme

@nascheme nascheme commented Aug 8, 2026

Copy link
Copy Markdown
Member

Two threads assigning to a special method of the same class could deadlock in the free-threaded build. If type_lock_prevent_release() only acts on TYPE_LOCK then we can get lock inversion. This happens when BEGIN_TYPE_DICT_LOCK() is used and the critical section holds both mutexes. We need to prevent release of both mutexes.

Two threads assigning to a special method of the same class could
deadlock in the free-threaded build.  If type_lock_prevent_release()
only acts on TYPE_LOCK then we can get lock inversion.  This happens
when BEGIN_TYPE_DICT_LOCK() is used and the critical section holds
both mutexes.  We need to prevent release of both mutexes.

Assisted-by: Claude
@nascheme

nascheme commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

This PR requires GH-155370 to be merged first otherwise the unit test added exposes data-races.

@nascheme

Copy link
Copy Markdown
Member Author

Setting as release blocker. When discussing this with @kumaraditya303, he suspects that some libraries could trigger this deadlock when they create types.

@nascheme
nascheme marked this pull request as ready for review August 12, 2026 15:19
@nascheme
nascheme requested a review from markshannon as a code owner August 12, 2026 15:19
@kumaraditya303
kumaraditya303 merged commit 50fcb91 into python:main Aug 15, 2026
58 checks passed
@kumaraditya303 kumaraditya303 added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 15, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @nascheme for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-app

bedevere-app Bot commented Aug 15, 2026

Copy link
Copy Markdown

GH-155822 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 15, 2026
@nascheme

Copy link
Copy Markdown
Member Author

Note that a unit test added by this PR exposes an existing leak, see gh-155978. This leak causes some refleak build bots to fail.

hugovk pushed a commit that referenced this pull request Aug 19, 2026
…55401) (#155822)

Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
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

Development

Successfully merging this pull request may close these issues.

2 participants