[issue47082] No protection: `import numpy` in two different threads can lead to race-condition

Sebastian Berg report at bugs.python.org
Mon Mar 21 19:04:38 EDT 2022


Sebastian Berg <sebastian at sipsolutions.net> added the comment:

Thanks, so there should already be a lock in place (sorry, I missed that).  But somehow we seem to get around it?

Do you know what may cause the locking logic to fail in this case?  Recursive imports in NumPy itself?  Or Cython using low-level C-API?

I.e. can you think of something to investigate that may help NumPy/Cython to make sure that locking is successful?


/* Cythons Import code (slightly cleaned up for Python 3 only): */
static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) {
    PyObject *empty_list = 0;
    PyObject *module = 0;
    PyObject *global_dict = 0;
    PyObject *empty_dict = 0;
    PyObject *list;
    if (from_list)
        list = from_list;
    else {
        empty_list = PyList_New(0);
        if (!empty_list)
            goto bad;
        list = empty_list;
    }
    global_dict = PyModule_GetDict(__pyx_m);
    if (!global_dict)
        goto bad;
    empty_dict = PyDict_New();
    if (!empty_dict)
        goto bad;
    {
        if (level == -1) {
            if ((1) && (strchr(__Pyx_MODULE_NAME, '.'))) {
                module = PyImport_ImportModuleLevelObject(
                    name, global_dict, empty_dict, list, 1);
                if (!module) {
                    if (!PyErr_ExceptionMatches(PyExc_ImportError))
                        goto bad;
                    PyErr_Clear();
                }
            }
            level = 0;
        }
        if (!module) {
            module = PyImport_ImportModuleLevelObject(
                name, global_dict, empty_dict, list, level);
        }
    }
bad:
    Py_XDECREF(empty_list);
    Py_XDECREF(empty_dict);
    return module;
}

----------

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


More information about the Python-bugs-list mailing list