mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
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()`.
This commit is contained in:
parent
c6dd2348ca
commit
052cb717f5
13 changed files with 109 additions and 81 deletions
|
@ -21,6 +21,10 @@ typedef struct _PyThreadStateImpl {
|
|||
// semi-public fields are in PyThreadState.
|
||||
PyThreadState base;
|
||||
|
||||
// The reference count field is used to synchronize deallocation of the
|
||||
// thread state during runtime finalization.
|
||||
Py_ssize_t refcount;
|
||||
|
||||
// These are addresses, but we need to convert to ints to avoid UB.
|
||||
uintptr_t c_stack_top;
|
||||
uintptr_t c_stack_soft_limit;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue