[issue31517] MainThread association logic is fragile
STINNER Victor
report at bugs.python.org
Thu Jul 4 06:19:46 EDT 2019
STINNER Victor <vstinner at redhat.com> added the comment:
Python internals already know who is the "main" thread: _PyRuntime.main_thread. It's maintained up to date, even after a fork, PyOS_AfterFork_Child() calls _PyRuntimeState_ReInitThreads() which does:
// This was initially set in _PyRuntimeState_Init().
runtime->main_thread = PyThread_get_thread_ident();
I already added _thread._is_main_interpreter() to deny spawning daemon threads in subinterpreters: bpo-37266.
We can add _thread._is_main_thread() which can reuse Modules/signalmodule.c code:
static int
is_main(_PyRuntimeState *runtime)
{
unsigned long thread = PyThread_get_thread_ident();
PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
return (thread == runtime->main_thread
&& interp == runtime->interpreters.main);
}
For example, this function is used by signal.signal:
if (!is_main(runtime)) {
PyErr_SetString(PyExc_ValueError,
"signal only works in main thread");
return NULL;
}
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31517>
_______________________________________
More information about the Python-bugs-list
mailing list