Skip to content

gh-162087: Release the first mutex of a two-mutex critical section that begins during stop-the-world - #162088

Open
maikschulze wants to merge 2 commits into
python:mainfrom
maikschulze:gh-162087-critical-section2-stw
Open

maikschulze wants to merge 2 commits into
python:mainfrom
maikschulze:gh-162087-critical-section2-stw

Conversation

@maikschulze

@maikschulze maikschulze commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

_PyCriticalSection2_BeginSlow() returns early while the interpreter's world is stopped (added for gh-144513) and records no mutexes, so _PyCriticalSection2_End() unlocks nothing. When the inline fast path calls it with is_m1_locked, the first mutex is already locked, and it stayed locked after the section had ended. As suggested in the issue, the early return now records the first mutex in the critical section in that case, so that _PyCriticalSection2_End() unlocks it.

The new test in _testinternalcapi begins a two-object critical section during a stop-the-world while the second object's mutex is held (standing in for a mutex handed off to a suspended thread), and checks that the first mutex is unlocked afterwards. It aborts on main and passes with the change. With the change the full test suite passes on macOS (free-threaded build).

The early return is in 3.15 and, since 3.14.4, in 3.14, so this may want backports. I couldn't find code that reaches this path today; details are in the issue.

I used an AI assistant to investigate this and draft the patch; I reviewed and tested the changes myself.

…tion is skipped during stop-the-world

_PyCriticalSection2_BeginSlow() returns early while the world is stopped
and records no mutexes, so _PyCriticalSection2_End() unlocks nothing. The
inline fast path has already locked the first mutex when it calls the slow
path with is_m1_locked set, and that mutex stayed locked after the section.
Unlock it in the early return, and add a test.
As suggested in review: when the world is stopped and the inline fast path
has already locked m1, record m1 in the critical section, so that
_PyCriticalSection2_End() unlocks it, instead of unlocking it in
_PyCriticalSection2_BeginSlow().
@maikschulze maikschulze changed the title gh-162087: Unlock the first mutex when a two-mutex critical section is skipped during stop-the-world gh-162087: Release the first mutex of a two-mutex critical section that begins during stop-the-world Nov 16, 2026
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.

1 participant