Skip to content

gh-146536: docs: Clarify that PyMutex is not re-entrant#146543

Open
dagangtj wants to merge 1 commit intopython:mainfrom
dagangtj:fix/issue-146536
Open

gh-146536: docs: Clarify that PyMutex is not re-entrant#146543
dagangtj wants to merge 1 commit intopython:mainfrom
dagangtj:fix/issue-146536

Conversation

@dagangtj
Copy link
Copy Markdown

@dagangtj dagangtj commented Mar 28, 2026

Summary

This PR clarifies the documentation for :c:type:PyMutex to explicitly state that it is not re-entrant (recursive).

Changes

  • Add note to :c:type:PyMutex type documentation explaining that it will deadlock if a thread attempts to acquire a lock it already holds
  • Add warning to :c:func:PyMutex_Lock about deadlock behavior on re-entry
  • Clarify :c:func:PyMutex_Unlock must be called by the same thread that acquired the lock

Background

The original issue reported confusion about whether PyMutex supports re-entrant locking. Since PyMutex occupies only one byte and uses atomic operations without tracking thread ownership, it cannot support re-entrancy. This documentation update makes this limitation explicit and suggests using :c:func:Py_BEGIN_CRITICAL_SECTION as an alternative for re-entrant locking scenarios.

Fixes #146536


📚 Documentation preview 📚: https://cpython-previews--146543.org.readthedocs.build/

Add explicit documentation that PyMutex is not re-entrant (recursive):

- Add note to PyMutex type explaining it will deadlock on re-entry
- Add warning to PyMutex_Lock about deadlock if already held
- Clarify PyMutex_Unlock must be called by the locking thread

Fixes python#146536
@dagangtj dagangtj requested a review from ZeroIntensity as a code owner March 28, 2026 02:46
@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news awaiting review labels Mar 28, 2026
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Mar 28, 2026
@StanFromIreland StanFromIreland changed the title docs: Clarify that PyMutex is not re-entrant gh-146536: docs: Clarify that PyMutex is not re-entrant Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

PyMutex re-entrancy not documented, confusing existing doc

1 participant