Requiring A Current Thread State for PyThreadState_Clear(), etc. is Problematic #101436
Labels
3.12
only security fixes
interpreter-core
(Objects, Python, Grammar, and Parser dirs)
topic-subinterpreters
type-bug
An unexpected behavior, bug, or error
In
PyThreadState_Clear()
andPyInterpreterState_Clear()
the code expects a "current" thread state (i.e. set byPyThreadState_Swap()
) to be set. Presumably this is for the following:For
PyThreadState_Clear()
, this is a problem when the current thread state is also the target thread state, since the function is progressively rendering the thread state unusable. ForPyInterpreterState_Clear()
the "current" thread state must belong to the target interpreter, but will be rendered unusable before it's potentially needed.(FWIW, it isn't clear to me yet why we don't see frequent crashes from this already (at least when we're clearing the main interpreter during finalization). I do suspect that this contributes to some of the occasional CI failures we see relative to finalization.)
Ideally auditing and the various object finalizers could work without having a current thread state, especially in the case where the current thread state could only possibly be the one getting cleared. Could we split up
PyThreadState
so at least the problematic part is guaranteed not be used inPyThreadState_Clear()
orPyInterpreterState_Clear()
? Are there other complexities we have to deal with?(Note that we'd still require that the GIL be held by the current OS thread when
PyThreadState_Clear()
orPyInterpreterState_Clear()
are called.)FTR, in gh-101308 I noted various cases where the lifecycle of thread states has an unexpected status. Pretty much all of them relate `PyThreadState_Clear()` requiring a current thread state.
Dealing with the main issue above would make it trivial to address the following:
PyThreadState_New()
PyThreadState_Clear()
should no longer be "active" or "bound"PyThreadState_Clear()
PyInterpreterState_Clear()
(for the main interpreter it must be the main thread)The text was updated successfully, but these errors were encountered: