]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.13] gh-87135: Hang non-main threads that attempt to acquire the GIL during finaliz...
authorGregory P. Smith <68491+gpshead@users.noreply.github.com>
Sun, 17 Aug 2025 16:08:14 +0000 (09:08 -0700)
committerGitHub <noreply@github.com>
Sun, 17 Aug 2025 16:08:14 +0000 (09:08 -0700)
commit9face218e7b640e52b83cd68cebb441d5ff0f8e2
treef19f945e99ac5119a424e698a4cf4cab1c7fb846
parentf2b7954ce0b07cceebea30d7a89d8a97d206d745
[3.13] gh-87135: Hang non-main threads that attempt to acquire the GIL during finalization (GH-105805) (GH-137827)

* [3.13] gh-87135: Hang non-main threads that attempt to acquire the GIL during finalization (GH-105805)

Instead of surprise crashes and memory corruption, we now hang threads that attempt to re-enter the Python interpreter after Python runtime finalization has started. These are typically daemon threads (our long standing mis-feature) but could also be threads spawned by extension modules that then try to call into Python. This marks the `PyThread_exit_thread` public C API as deprecated as there is no plausible safe way to accomplish that on any supported platform in the face of things like C++ code with finalizers anywhere on a thread's stack. Doing this was the least bad option.

(cherry picked from commit 8cc5aa47ee464ddfd8da5461edecf4a5c72df2ff)

Co-authored-by: Jeremy Maitin-Shepard <jeremy@jeremyms.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
* state "3.13.7 and earlier"
* backport: do not add the deprecated marker
* fix Py_IsFinalizing doc ref

---------

Co-authored-by: Jeremy Maitin-Shepard <jeremy@jeremyms.com>
Doc/c-api/init.rst
Include/internal/pycore_pythread.h
Include/pythread.h
Lib/test/test_threading.py
Misc/NEWS.d/next/C API/2022-08-05-19-41-20.gh-issue-87135.SCNBYj.rst [new file with mode: 0644]
Modules/_testcapimodule.c
Python/ceval_gil.c
Python/pylifecycle.c
Python/thread_nt.h
Python/thread_pthread.h