-
-
Notifications
You must be signed in to change notification settings - Fork 9.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
segfault when importing numpy after reinitializing Python #11925
Comments
I believe this is a known problem with Python. @njsmith Comment? |
It's a know issue that "dynamically loaded extension modules loaded by Python are not unloaded" and " some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_FinalizeEx() more than once." [Py_FinalizeEx documentation] It seems like NumPy is one of those extension modules that crashes when the initialization function is called twice. As long as I don't use NumPy, everything works (or seems to work) as expected after resetting the interpreter. |
There is already a similar bug report regarding reimport of NumPy after reinitialization: #8097 |
Is my understanding correct that the root cause of the problem is that parts of NumPy do not implement the
For example the _multiarray_umath module, does not support it, as we can see from the NULLs in the module definition in multiarraymodule.c: static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_multiarray_umath",
NULL,
-1,
array_module_methods,
NULL,
NULL,
NULL,
NULL
}; Are there plans to use multi-phase initialization in the future? |
This prevents a crash when you reload the script when using python 3 if numpy is installed. When weechat unloads a script it calls Py_Finalize, and when it loads a script, it calls Py_Initialize. If numpy is imported, this causes python to segfault, which crashes weechat. By setting numpy in sys.modules to None, it won't be imported when websocket-client tries to import it. numpy is an optional dependency of websocket-client, and if it's not installed, it will set numpy to None itself, so therefore, this should work just like when numpy isn't installed. See numpy/numpy#11925 for more details about the issue in numpy.
I embedded Python into a C program. At certain points in time, the C program resets the interpreter by calling Py_Finalize + Py_Initialize. When the evaluated code imports NumPy before and after the reset, then the second import fails with a segmentation fault.
Reproducing code example:
At first the example C program initializes Python, imports NumPy, and then finalizes Python.
Then it reinitializes Python and imports NumPy again. When importing NumPy
for the second time, the import fails with a segmentation fault.
Tested with Debian 9, Python 3.7.0 valgrind friendly debug build, NumPy debug build of latest
version from github (1.16.0.dev0+e796449). The bug reproduces on Mac (tested with Python 3.5.1,
NumPy 1.11.0) and Windows (Tested with Python 3.7.0 from Python.org, NumPy 1.14.5).
Error message:
Segmentation fault on evaluation of "import numpy" after reinitialization of Python. Output of valgrind:
Numpy/Python version information:
Python 3.7.0 (default, Aug 27 2018, 19:39:47) [GCC 6.3.0 20170516]
Numpy 1.16.0.dev0+e796449
The text was updated successfully, but these errors were encountered: