-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
Description
PyThreadState_Clear() includes the following comment:
Lines 1553 to 1558 in 1e4680c
| /* XXX Conditions we need to enforce: | |
| * the GIL must be held by the current thread | |
| * current_fast_get()->interp must match tstate->interp | |
| * for the main interpreter, current_fast_get() must be the main thread | |
| */ |
We should enforce this, particularly the comment about the matching interpreters. Calling PyThreadState_Clear() from the "wrong" interpreter is unsafe because if any of the PyObjects on the tstate are not NULL, calling their destructors from the wrong thread can lead to memory corruption.
This is also important for the "free threaded" builds because they have free lists associated with the PyThreadState and these will be cleared in PyThreadState_Clear() -- doing this in the wrong interpreter leads to memory corruption.
There are currently two places which call PyThreadState_Clear() from the "wrong" interpreter:
interp_create()in_xxsubinterpretersmodule.c. This is pretty easy to fix by setting the thread state before calling clear. `cpython/Modules/_xxsubinterpretersmodule.c
Line 266 in 1e4680c
PyThreadState_Clear(tstate); new_interpreter()inpylifecycle.cin the error code path. This is trickier because the thread state is not fully initialized.
Line 2164 in 1e4680c
PyThreadState_Clear(tstate);
Related: #101436 (comment)
Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
Status