Title: gh-148820: Fix `_PyRawMutex` use-after-free on spurious semaphore wakeup by colesbury · Pull Request #148847 · python/cpython · GitHub
Open Graph Title: gh-148820: Fix `_PyRawMutex` use-after-free on spurious semaphore wakeup by colesbury · Pull Request #148847 · python/cpython
X Title: gh-148820: Fix `_PyRawMutex` use-after-free on spurious semaphore wakeup by colesbury · Pull Request #148847 · python/cpython
Description: _PyRawMutex_UnlockSlow CAS-removes the waiter from the list and then calls _PySemaphore_Wakeup, with no handshake (unlike parking_lot's wait_entry.is_unparking protocol). If the waiter's _PySemaphore_Wait returns for any reason other than the matching Wakeup — e.g. Py_PARK_INTR from sigint_event on the main thread on Windows, or EINTR on POSIX — the waiter can exit the wait, re-acquire the mutex (already CAS-unlocked by the unlocker), break out of the loop, and call _PySemaphore_Destroy on the stack-allocated semaphore before the unlocker's _PySemaphore_Wakeup runs. That Wakeup then hits a closed handle, producing Fatal Python error: _PySemaphore_Wakeup: parking_lot: ReleaseSemaphore failed (Win32 ERROR_INVALID_HANDLE). Observed on free-threaded 3.14 on Windows with coverage run + trio + signal.raise_signal(SIGINT) from a non-main thread. Parking-lot's _PyParkingLot_Park is not affected because its bucket-locked is_unparking handshake already ensures the parker waits for the unparker's Wakeup before destroying the semaphore. Fix Loop in _PyRawMutex_LockSlow until _PySemaphore_Wait returns Py_PARK_OK. Py_PARK_OK is only returned when a matching post was observed on all backends (WAIT_OBJECT_0 / sem_wait success / sema->counter > 0), so looping until OK guarantees the unlocker's pending Wakeup has fired before we destroy the semaphore. Deferring SIGINT until the unlock completes is acceptable in practice: _PyRawMutex protects only short critical sections (principally parking-lot bucket locks), so ctrl-C is delivered near-immediately. Also include GetLastError() and the HANDLE value in the Windows fatal messages in _PySemaphore_Init, _PySemaphore_Wait, and _PySemaphore_Wakeup — these were key in identifying the bug as a use-after-free (error 6 = ERROR_INVALID_HANDLE) rather than a double-release. The _PySemaphore_Wakeup API signature is intentionally left unchanged to preserve the ability to backport this fix. Based on analysis and patch by @colesbury in https://gist.github.com/colesbury/3e4c6180e3eb4b3b9fd07b26f3196e12 / https://gist.github.com/colesbury/f9f0c5cf1a00f2c946e80795fcc245d7. Verification Stress harness (16 workers × 100 iterations × coverage run of the trio SIGINT-from-thread reproducer) showed ~0.19% crash rate on the unpatched baseline and 0% with the loop. Issue: gh-148820
Open Graph Description: _PyRawMutex_UnlockSlow CAS-removes the waiter from the list and then calls _PySemaphore_Wakeup, with no handshake (unlike parking_lot's wait_entry.is_unparking protocol). If the waiter's _P...
X Description: _PyRawMutex_UnlockSlow CAS-removes the waiter from the list and then calls _PySemaphore_Wakeup, with no handshake (unlike parking_lot's wait_entry.is_unparking protocol). If the waiter&...
Opengraph URL: https://github.com/python/cpython/pull/148847
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:ebee146b-2376-6f6b-f5f0-acb36f7df0a0 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C84E:8AF77:24E86A:324B0C:6A57FA88 |
| html-safe-nonce | e7fd871187b69a5cdb39441aeedf56c9100ae82e0486b7a5b908e93bfdd8f99f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODRFOjhBRjc3OjI0RTg2QTozMjRCMEM6NkE1N0ZBODgiLCJ2aXNpdG9yX2lkIjoiNTAyODc4OTQzNjk5NTMzNjg0MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ad5e6085ad9bef95db3b317f0ff9fc7951caad5fdb4ed4ac751b8093fc7ed0c5 |
| hovercard-subject-tag | pull_request:3563481419 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/148847/files |
| twitter:image | https://avatars.githubusercontent.com/u/655866?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/655866?s=400&v=4 |
| og:image:alt | _PyRawMutex_UnlockSlow CAS-removes the waiter from the list and then calls _PySemaphore_Wakeup, with no handshake (unlike parking_lot's wait_entry.is_unparking protocol). If the waiter's _P... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 49c8c15fabcbf356d607a90ca115c13b273e42ff8b74155de050fd229a9b0121 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3fb1f684e7a833eb1b2d01d39875a2b52cb4fe9b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width