thread_run(void *boot_raw)
{
struct bootstate *boot = (struct bootstate *) boot_raw;
- PyThreadState *tstate;
+ PyThreadState *tstate = boot->tstate;
+
+ // gh-104690: If Python is being finalized and PyInterpreterState_Delete()
+ // was called, tstate becomes a dangling pointer.
+ assert(_PyThreadState_CheckConsistency(tstate));
- tstate = boot->tstate;
tstate->thread_id = PyThread_get_thread_ident();
#ifdef PY_HAVE_THREAD_NATIVE_ID
tstate->native_thread_id = PyThread_get_thread_native_id();
"cannot access free variable '%s' where it is not associated with a" \
" value in enclosing scope"
-#ifndef NDEBUG
-/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
- PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
- when a thread continues to run after Python finalization, especially
- daemon threads. */
-static int
-is_tstate_valid(PyThreadState *tstate)
-{
- assert(!_PyMem_IsPtrFreed(tstate));
- assert(!_PyMem_IsPtrFreed(tstate->interp));
- return 1;
-}
-#endif
-
/* This can set eval_breaker to 0 even though gil_drop_request became
1. We believe this is all right because the eval loop will release
void
PyEval_ReleaseThread(PyThreadState *tstate)
{
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
_PyRuntimeState *runtime = tstate->interp->runtime;
PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
static int
handle_signals(PyThreadState *tstate)
{
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
return 0;
}
_Py_FinishPendingCalls(PyThreadState *tstate)
{
assert(PyGILState_Check());
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
struct _pending_calls *pending = &tstate->interp->ceval.pending;
assert(PyGILState_Check());
PyThreadState *tstate = _PyThreadState_GET();
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
/* Python signal handler doesn't really queue a callback: it only signals
that a signal was received, see _PyEval_SignalReceived(). */
int
_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
{
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
/* The caller must hold the GIL */
assert(PyGILState_Check());
int
_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
{
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
/* The caller must hold the GIL */
assert(PyGILState_Check());
/* Not switched yet => wait */
if (((PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) == tstate)
{
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
RESET_GIL_DROP_REQUEST(tstate->interp);
/* NOTE: if COND_WAIT does not atomically start waiting when
releasing the mutex, another thread can run through, take
PyThread_exit_thread();
}
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
PyInterpreterState *interp = tstate->interp;
struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
struct _ceval_state *ceval2 = &interp->ceval;
}
PyThread_exit_thread();
}
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
SET_GIL_DROP_REQUEST(interp);
drop_requested = 1;
drop_gil(ceval, ceval2, tstate);
PyThread_exit_thread();
}
- assert(is_tstate_valid(tstate));
+ assert(_PyThreadState_CheckConsistency(tstate));
if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
RESET_GIL_DROP_REQUEST(interp);
}
+#ifndef NDEBUG
+// Check that a Python thread state valid. In practice, this function is used
+// on a Python debug build to check if 'tstate' is a dangling pointer, if the
+// PyThreadState memory has been freed.
+//
+// Usage:
+//
+// assert(_PyThreadState_CheckConsistency(tstate));
+int
+_PyThreadState_CheckConsistency(PyThreadState *tstate)
+{
+ assert(!_PyMem_IsPtrFreed(tstate));
+ assert(!_PyMem_IsPtrFreed(tstate->interp));
+ return 1;
+}
+#endif
+
+
#ifdef __cplusplus
}
#endif