Skip to content

[3.14] gh-148820: Fix _PyRawMutex use-after-free on spurious semaphore wakeup (gh-148852)#148884

Merged
colesbury merged 1 commit intopython:3.14from
colesbury:backport-ad3c5b7-3.14
Apr 22, 2026
Merged

[3.14] gh-148820: Fix _PyRawMutex use-after-free on spurious semaphore wakeup (gh-148852)#148884
colesbury merged 1 commit intopython:3.14from
colesbury:backport-ad3c5b7-3.14

Conversation

@colesbury
Copy link
Copy Markdown
Contributor

@colesbury colesbury commented Apr 22, 2026

_PyRawMutex_UnlockSlow CAS-removes the waiter from the list and then calls _PySemaphore_Wakeup, with no handshake. If _PySemaphore_Wait returns Py_PARK_INTR, the waiter can destroy its stack-allocated semaphore before the unlocker's Wakeup runs, causing a fatal error from ReleaseSemaphore / sem_post.

Loop in _PyRawMutex_LockSlow until _PySemaphore_Wait returns Py_PARK_OK, which is only signalled when a matching Wakeup has been observed.

Also include GetLastError() and the handle in the Windows fatal messages in _PySemaphore_Init, _PySemaphore_Wait, and _PySemaphore_Wakeup to make similar races easier to diagnose in the future.

(cherry picked from commit ad3c5b7)

…maphore wakeup (pythongh-148852)

_PyRawMutex_UnlockSlow CAS-removes the waiter from the list and then
calls _PySemaphore_Wakeup, with no handshake. If _PySemaphore_Wait
returns Py_PARK_INTR, the waiter can destroy its stack-allocated
semaphore before the unlocker's Wakeup runs, causing a fatal error from
ReleaseSemaphore / sem_post.

Loop in _PyRawMutex_LockSlow until _PySemaphore_Wait returns Py_PARK_OK,
which is only signalled when a matching Wakeup has been observed.

Also include GetLastError() and the handle in the Windows fatal messages
in _PySemaphore_Init, _PySemaphore_Wait, and _PySemaphore_Wakeup to make
similar races easier to diagnose in the future.
(cherry picked from commit ad3c5b7)

Co-authored-by: Sam Gross <colesbury@gmail.com>
@colesbury colesbury enabled auto-merge (squash) April 22, 2026 18:48
@colesbury colesbury merged commit e5d5541 into python:3.14 Apr 22, 2026
52 checks passed
@colesbury colesbury deleted the backport-ad3c5b7-3.14 branch April 22, 2026 19:10
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