[docs] [issue31517] MainThread association logic is fragile

STINNER Victor report at bugs.python.org
Thu Jul 4 06:19:47 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)) {
                        "signal only works in main thread");
        return NULL;


Python tracker <report at bugs.python.org>

More information about the docs mailing list