[Python-Dev] [RELEASE] Python 3.8.0a1 is now available for testing

Victor Stinner vstinner at redhat.com
Thu Feb 28 20:35:20 EST 2019


Hi,

Le ven. 1 mars 2019 à 02:12, Neil Schemenauer <nas-python at arctrix.com> a écrit :
> I believe the correct fix is to use PEP 3121 per-interpreter module
> state.  I created a new issue:
>
>     https://github.com/psycopg/psycopg2/issues/854
>
> I think the fix is not trival as the psycopgmodule.c source code has
> change a fair bit to use the PEP 3121 APIs.

The problem is this function:


/* Return nonzero if the current one is the main interpreter */
static int
psyco_is_main_interp(void)
{
    static PyInterpreterState *main_interp = NULL;  /* Cached reference */
    PyInterpreterState *interp;

    if (main_interp) {
        return (main_interp == PyThreadState_Get()->interp);
    }

    /* No cached value: cache the proper value and try again. */
    interp = PyInterpreterState_Head();
    while (interp->next)
        interp = interp->next;

    main_interp = interp;
    assert (main_interp);
    return psyco_is_main_interp();
}

https://github.com/psycopg/psycopg2/blob/599432552aae4941c2b282e9251330f1357b2a45/psycopg/utils.c#L407

I'm not sure that this code is safe. In CPython, iterating on
interp->next is protected by a lock:

    HEAD_LOCK();
    ...
    HEAD_UNLOCK();

We already expose the main interpreter since Python 3.7:
PyInterpreterState_Main(). psycopg can be modified to use directly
this function rather than playing black magic with CPython internals.

IMHO it's a good thing that the compilation failed: that such bug is found :-)

Victor
-- 
Night gathers, and now my watch begins. It shall not end until my death.


More information about the Python-Dev mailing list