]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-112723: Call `PyThreadState_Clear()` from the correct interpreter (#112776)
authorSam Gross <colesbury@gmail.com>
Wed, 13 Dec 2023 00:20:21 +0000 (19:20 -0500)
committerGitHub <noreply@github.com>
Wed, 13 Dec 2023 00:20:21 +0000 (17:20 -0700)
commita3c031884d2f16d84aacc3f733c047b3a6cae208
treedb560340456c0b7ec034e3c03382ca3d077b9628
parent8a4c1f3ff1e3d7ed2e00e77b94056f9bb7f9ae3b
gh-112723: Call `PyThreadState_Clear()` from the correct interpreter (#112776)

The `PyThreadState_Clear()` function must only be called with the GIL
held and must be called from the same interpreter as the passed in
thread state. Otherwise, any Python objects on the thread state may be
destroyed using the wrong interpreter, leading to memory corruption.

This is also important for `Py_GIL_DISABLED` builds because free lists
will be associated with PyThreadStates and cleared in
`PyThreadState_Clear()`.

This fixes two places that called `PyThreadState_Clear()` from the wrong
interpreter and adds an assertion to `PyThreadState_Clear()`.
Include/internal/pycore_ceval.h
Include/internal/pycore_pylifecycle.h
Modules/_xxsubinterpretersmodule.c
Python/ceval_gil.c
Python/pylifecycle.c
Python/pystate.c