-
-
Notifications
You must be signed in to change notification settings - Fork 11k
BUG: Calling import numpy
at the same time in two different threads can lead to a race-condition
#21223
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
Comments
@jhgoebbert I would expect Python ensures that importing is thread-safe? It would seem error prone to allow importing the same module twice: Even for simple Python modules running the side-effects of importing more than once seems potentially wrong. I am really curious how the multiple threads are created in this example? Are there sub-interpreters or so involved? |
The problem might be that |
Simply using threading.Thread If |
@totaam I don't think
print("running import of 'imported'!")
import time
time.sleep(1)
print("finished import of 'imported'!") and
And as expected running
I assume there isn't any other complexities, i.e. PyPy involved? And I don't really understand how anything could be messing with environment variables. So, my current hypothesis (and I have briefly checked the Python code) is that Python does not do manual locking. But it effectively locks due to this going into C and thus holding the GIL. But somewhere during the import of NumPy, NumPy probably releases the GIL briefly and that could allow the next thread to go into the import machinery. Would you be up to opening a bug with Python? NumPy may be doing some worse than typical stuff here, but right now it seems to me that Python should be protecting us. If Python says that this is expected, I am wondering if you would have to do the locking somehow. Since you are managing the threads. All of that doesn't mean we can't consider adding a band-aid fix to help in NumPy. |
cpython does not allow to open issues on GitHub. So I have started a new topic on |
Oh, they are finalizing the process of moving from https://bugs.python.org to github, I guess it might be a bit confusing right now :). |
Not that I can think of, no. Definitely no PyPy involved. |
Oh, I moved it to the issues https://bugs.python.org/issue47082 |
Describe the issue:
Calling
import numpy
at the same time in two different threads can lead to a race-conditionThis happens for example with Xpra when loading the encoder nvjpeg:
The problem seems to come from numpy directly.
Here the environment variable OPENBLAS_MAIN_FREE is set:
https://github.com/numpy/numpy/blob/maintenance/1.21.x/numpy/core/__init__.py#L18
and short after that it is deleted
https://github.com/numpy/numpy/blob/maintenance/1.21.x/numpy/core/__init__.py#L51
But this deletion fails ...
To me this looks like a threading issue in numpy. A lock would need to be set here.
Reproduce the code example:
Error message:
The text was updated successfully, but these errors were encountered: