-
-
Notifications
You must be signed in to change notification settings - Fork 12.2k
BUG: Calling import numpy at the same time in two different threads can lead to a race-condition #21223
Copy link
Copy link
Closed
Labels
Description
Describe the issue:
Calling import numpy at the same time in two different threads can lead to a race-condition
This happens for example with Xpra when loading the encoder nvjpeg:
2022-03-20 12:54:59,298 cannot load enc_nvjpeg (nvjpeg encoder)
Traceback (most recent call last):
File "<pythondir>/lib/python3.9/site-packages/xpra/codecs/loader.py", line 52, in codec_import_check
ic = __import__(class_module, {}, {}, classnames)
File "xpra/codecs/nvjpeg/encoder.pyx", line 8, in init xpra.codecs.nvjpeg.encoder
File "<pythondir>/lib/python3.9/site-packages/numpy/__init__.py", line 150, in <module>
from . import core
File "<pythondir>/lib/python3.9/site-packages/numpy/core/__init__.py", line 51, in <module>
del os.environ[envkey]
File "<pythondir>/lib/python3.9/os.py", line 695, in __delitem__
raise KeyError(key) from None
KeyError: 'OPENBLAS_MAIN_FREE'
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:
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
If two threads call this funtion at the same time we get a race-condition.
The deletion fails ...Error message:
File "<pythondir>/lib/python3.9/site-packages/numpy/core/__init__.py", line 51, in <module>
del os.environ[envkey]
File "<pythondir>/lib/python3.9/os.py", line 695, in __delitem__
raise KeyError(key) from None
KeyError: 'OPENBLAS_MAIN_FREE'
### NumPy/Python version information:
numpy 1.21.3
Python 3.9.6
Reactions are currently unavailable