mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Fix another race
This commit is contained in:
parent
c3b4807dac
commit
2e197658cc
1 changed files with 13 additions and 2 deletions
|
|
@ -4135,14 +4135,25 @@ register_lazy_on_parent(PyThreadState *tstate, PyObject *name, PyObject *builtin
|
||||||
PyObject *child = NULL;
|
PyObject *child = NULL;
|
||||||
PyObject *parent_module = NULL;
|
PyObject *parent_module = NULL;
|
||||||
PyObject *parent_dict = NULL;
|
PyObject *parent_dict = NULL;
|
||||||
PyObject *lazy_modules = tstate->interp->imports.lazy_modules;
|
|
||||||
|
// Acquire import lock to safely initialize lazy_modules if needed
|
||||||
|
// This prevents a race where multiple threads could create different dicts
|
||||||
|
PyInterpreterState *interp = tstate->interp;
|
||||||
|
_PyImport_AcquireLock(interp);
|
||||||
|
|
||||||
|
PyObject *lazy_modules = interp->imports.lazy_modules;
|
||||||
if (lazy_modules == NULL) {
|
if (lazy_modules == NULL) {
|
||||||
lazy_modules = tstate->interp->imports.lazy_modules = PyDict_New();
|
lazy_modules = interp->imports.lazy_modules = PyDict_New();
|
||||||
if (lazy_modules == NULL) {
|
if (lazy_modules == NULL) {
|
||||||
|
_PyImport_ReleaseLock(interp);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Release the lock - we only needed it for initialization
|
||||||
|
// The dict operations below are thread-safe on their own
|
||||||
|
_PyImport_ReleaseLock(interp);
|
||||||
|
|
||||||
Py_INCREF(name);
|
Py_INCREF(name);
|
||||||
while (true) {
|
while (true) {
|
||||||
Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, PyUnicode_GET_LENGTH(name), -1);
|
Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, PyUnicode_GET_LENGTH(name), -1);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue