[New-bugs-announce] [issue35388] _PyRuntime_Initialize() called after Py_Finalize() does nothing

STINNER Victor report at bugs.python.org
Mon Dec 3 10:13:10 EST 2018


New submission from STINNER Victor <vstinner at redhat.com>:

When Python is embedded, it should be possible to call the following Python function multiple times:

void func(void)
{
  Py_Initialize();
  /* do something in Python */
  Py_Finalize();
}

Py_Finalize() ends by calling _PyRuntime_Finalize().

Problem: when Py_Initialize() is called the second time, _PyRuntime_Initialize() does nothing:

_PyInitError
_PyRuntime_Initialize(void)
{
    /* XXX We only initialize once in the process, which aligns with
       the static initialization of the former globals now found in
       _PyRuntime.  However, _PyRuntime *should* be initialized with
       every Py_Initialize() call, but doing so breaks the runtime.
       This is because the runtime state is not properly finalized
       currently. */
    static int initialized = 0;
    if (initialized) {
        return _Py_INIT_OK();
    }
    initialized = 1;

    return _PyRuntimeState_Init(&_PyRuntime);
}

For example, Py_Finalize() clears runtime->interpreters.mutex and runtime->xidregistry.mutex, whereas mutexes are still needed the second time func() is called.

There is currently a *workaround*:

_PyInitError
_PyInterpreterState_Enable(_PyRuntimeState *runtime)
{
    ...
    if (runtime->interpreters.mutex == NULL) {
        ...
        runtime->interpreters.mutex = PyThread_allocate_lock();
        ...
    }
    ...
}

I would prefer that _PyRuntime_Initialize() calls _PyRuntimeState_Init() each time, and that _PyRuntimeState_Init() does nothing at the following call (except after Py_Finalize?).

Note: _PyRuntimeState_Fini() doesn't free runtime->xidregistry.mutex currently.

----------
messages: 330946
nosy: vstinner
priority: normal
severity: normal
status: open
title: _PyRuntime_Initialize() called after Py_Finalize() does nothing
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35388>
_______________________________________


More information about the New-bugs-announce mailing list