-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Crash calling import_array() in C++-Api when python runs in separate thread #21588
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 agr 8000 ee to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I am not sure about the subtleties that may exist if a Python interpreter is initialized in a thread itself. But I doubt that this has anything to do with NumPy here. Maybe check: https://docs.python.org/3/c-api/init.html#non-python-created-threads I suspect you must add that thread management, but that probably it is OK to keep the |
Thank you for your answer. Yes, I suspected that with the GIL, but without success. I called PyGILState_Ensure() right after Py_Initialize(), but the crash is unavoidable. What confuses me a lot about this is that the problem cannot be reproduced on Windows, Ubuntu, Fedora and Raspbian.... |
Maybe try using a newer NumPy version? I guess the problem may just be the M1 here then? Are you running in the compatibility mode? |
Okay this might be worth trying :) I will report when I have tried |
Updating NumPy to 1.22.4 didn't fix the issue... |
I am having a similar crash, exactly at import_array(). But it is related to how python loaded hashlib (openssl) library. /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 --preload /usr/lib/x86_64-linux-gnu/libssl.so ./upsNativeInterfence stacktrace hint:
|
I will check if this solves my issue… |
I've done some researching on this on the CPython end and this seems to be related to OpenBlas' threading support. If I run the program with OPENBLAS_NUM_THREADS set to 1 in the shell environment it does not crash, without the environment variable or with a higher number of threads I get a bus error as described in this issue. And while writing this: The issue might be related to the stack size given the top of the stack trace. The default stack size for threads on macOS is fairly small. In CPython we explicitly set the stack size for threads we create in the threading module to a much larger value.
|
After some googling I found that std::thread does not provide an option to set the stack size. QThreads have such an option. Increasing the thread size seems to fix the problem for now :). I raised the thread size to 8MB and the error was gone. Still, it's not really satisfying that this is not possible with std::threads. Does anyone know another possibility? |
Can confirm that the following fixes the problem:
The only alternative I found was to launch create the thread via |
I also can confirm success on macOS Ventura 13.1 (ARM/M1) and Python 3.9.13. I created a lightweight replacement for std::thread that uses pthreads, set the stack size to 768K (more than recommended above, just to be safe) and the crash is solved. Many thanks to all above who worked through this issue and found a solution. ;-) |
I just want to mention that I got a similar (or exactly the same?) crash involving libcrypto, Python, TensorFlow and Numpy. It helped a lot to use You also find a similar crash in pybind/pybind11#3543. With linking TF, I got outputs like this:
Just running
As you see, in the case with TF, it finds those OpenSSL related symbols in Our solution here was to add |
Sounds like all the issues mentioned here were fixed in some form and not related to NumPy directly. So closing, please open a new issue if I missed something. |
Describe the issue:
I get an EXC_BAD_ACCESS Error when calling import_array() in an c++ program if Python runs in an different thread.
In detail I have written a module that uses Numpy. If I initialize Python in my main thread and import my module everything works fine. But if I initialize Python in an different thread and import the module in this thread, numpy crashes when calling import_array(). More precise the crash happens wenn PyImport_ImportModule("numpy.core._multiarray_umath") is called in _import_array() (_multiarray_api.h).
I'm using macOS Monterey 12.3.1 on an Apple Silicon (M1) processor. My Python version is 3.9.6 and the NumPy I'm using is 1.21.4. Python and Numpy are for ARM architecture. As compiler I'm using Clang 12.0.5.
The issue can also be reproduced on Mac machines with intel processors
On Windows no problem occurs.
Reproduce the code example:
Error message:
NumPy/Python version information:
1.21.4 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:14:58)
[Clang 12.0.5 (clang-1205.0.22.9)]
The text was updated successfully, but these errors were encountered: