]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-124878: Fix race conditions during interpreter finalization (#130649)
authorSam Gross <colesbury@gmail.com>
Thu, 6 Mar 2025 15:38:34 +0000 (10:38 -0500)
committerGitHub <noreply@github.com>
Thu, 6 Mar 2025 15:38:34 +0000 (10:38 -0500)
commit052cb717f5f97d08d2074f4118fd2c21224d3015
treeccdcdd01ce37cd3ae2530ca54a476486a73a1c65
parentc6dd2348ca61436fc1444ecc0343cb24932f6fa7
gh-124878: Fix race conditions during interpreter finalization (#130649)

The PyThreadState field gains a reference count field to avoid
issues with PyThreadState being a dangling pointer to freed memory.
The refcount starts with a value of two: one reference is owned by the
interpreter's linked list of thread states and one reference is owned by
the OS thread. The reference count is decremented when the thread state
is removed from the interpreter's linked list and before the OS thread
calls `PyThread_hang_thread()`. The thread that decrements it to zero
frees the `PyThreadState` memory.

The `holds_gil` field is moved out of the `_status` bit field, to avoid
a data race where on thread calls `PyThreadState_Clear()`, modifying the
`_status` bit field while the OS thread reads `holds_gil` when
attempting to acquire the GIL.

The `PyThreadState.state` field now has `_Py_THREAD_SHUTTING_DOWN` as a
possible value. This corresponds to the `_PyThreadState_MustExit()`
check. This avoids race conditions in the free threading build when
checking `_PyThreadState_MustExit()`.
13 files changed:
Include/cpython/pystate.h
Include/internal/pycore_pystate.h
Include/internal/pycore_runtime_init.h
Include/internal/pycore_tstate.h
Misc/NEWS.d/next/Core_and_Builtins/2025-02-27-18-48-42.gh-issue-124878.DS0MIL.rst [new file with mode: 0644]
Modules/_threadmodule.c
Modules/posixmodule.c
Python/ceval_gil.c
Python/pylifecycle.c
Python/pystate.c
Python/qsbr.c
Tools/tsan/suppressions.txt
Tools/tsan/suppressions_free_threading.txt