-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PyImport_GetModule() can return partially-initialized module #80124
Comments
PyImport_GetModule() returns whatever is in sys.modules, even if the module is still importing and therefore only partially initialized. One possibility is to reuse the optimization already done in PyImport_ImportModuleLevelObject(): /* Optimization: only call _bootstrap._lock_unlock_module() if
__spec__._initializing is true.
NOTE: because of this, initializing must be set *before*
stuffing the new module in sys.modules.
*/
spec = _PyObject_GetAttrId(mod, &PyId___spec__);
if (_PyModuleSpec_IsInitializing(spec)) {
PyObject *value = _PyObject_CallMethodIdObjArgs(interp->importlib,
&PyId__lock_unlock_module, abs_name,
NULL);
if (value == NULL) {
Py_DECREF(spec);
goto error;
}
Py_DECREF(value);
}
Py_XDECREF(spec); Issue originally mentioned in bpo-34572. |
Yeah, that makes sense. |
Do we plan to backport the change by nanjekyejoannah to 3.7 branch? |
I've assigned this to Joannah to decide if she wants to backport this. |
Thanks. Is it possible that this issue and https://bugs.python.org/issue38884 are duplicates? |
The changes required to successfully do this backport are many and affect critical areas. I am not in a hurry to do this. If anyone else wants to take this up quickly, please do. |
Do you mean that there is a risk that the backport introduces a regression in another part of the code? If yes, I would suggest to not backport the change to *stable* branches. People survived with bug. Do you really *have to* backport the fix? Note: this issue is closed. If you consider to backport it, I suggest to reopen the issue. |
My worry are the many changes that are required to ceval to make this back port work. Not that I think we can not successfully backport things. we can. |
I have been experiencing what I thought was this issue in my embedded Python code. We have been using Python 3.7, so I thought upgrading to 3.8.1 would fix it, but it doesn't seem to have made any difference. My C++ code essentially can call PyImport_GetModule() from two threads simultaneously on the same module A. The symptoms I see are that one of them then gets a stacktrace in module B (imported by A), saying that some symbol defined near the end of B does not exist. I've also noticed that this happens far more often on deployed code (where Python modules end up in a zip file) than when run directly in development (where the modules are just normal files). I can't see any difference in the frequency between 3.7.5 and 3.8.1. Any ideas? Should I reopen this? |
Oops, I mean we call PyImport_ImportModule and get these issues when the files are zipped. Unless that calls PyImport_GetModule internally I guess it's not related to this then. |
@gjb1002: see also https://bugs.python.org/issue38884, which demonstrates that concurrent imports are not thread-safe on Python 3. |
@Valentyn Tymofieiev - true, and thanks for the tip, though the symptoms described there are somewhat different from what I'm observing. Also, my problem seems to be dependent on zipping the Python code, which that one isn't. |
After this fix, some functions like multiprocessing.Pool cannot be used in threaded code(https://bugs.python.org/issue41567). importerror-sample.tgz contains simplified code to reproduce the same error without multiprocessing module. Is this an expected behaviour of this change? Tested with Python 3.9.0/macOS 10.15.5. |
Is this bug causing the Dask-Jupyterlab failure ? |
Note the conjunction of this change + bpo-32596 produces import fragility: |
Ok, going through other open issues including on third-party projects, I think these changes should unfortunately be reverted. The regressions produced are far from trivial and most developers seem at a loss how to fix them. |
After analysis, it may not need reversal. There is a simple logic error it seems. Will check. |
Created a new issue + fix in bpo-43517. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: