]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-148820: Fix _PyRawMutex use-after-free on spurious semaphore wakeup (gh-148852)
authorSam Gross <colesbury@gmail.com>
Wed, 22 Apr 2026 18:31:19 +0000 (14:31 -0400)
committerGitHub <noreply@github.com>
Wed, 22 Apr 2026 18:31:19 +0000 (14:31 -0400)
commitad3c5b7958b890382f431a53349320cb7c84d405
tree6449c003259971982b2857ae074ecf7d943bcc0f
parent59b41c8c3ba3251f15e6b58d9793d72499b298c0
gh-148820: Fix _PyRawMutex use-after-free on spurious semaphore wakeup (gh-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.
Misc/NEWS.d/next/Core_and_Builtins/2026-04-21-14-36-44.gh-issue-148820.XhOGhA.rst [new file with mode: 0644]
Python/lock.c
Python/parking_lot.c