Possible thread-unsafe initialization of wrapped modules?
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Reproduce the concurrent script and compare it with scipy/_lib/tests/test_public_api.py::test_all_modules_are_expected. Then inspect clone_module in array_api_compat/_internal.py at the linked lines, focusing on concurrent initialization. Done means the multi-threaded walk_packages example completes without the reported KeyError.
Written by the indexing model from the issue text.
Description
Consider the following script:
from concurrent.futures import ThreadPoolExecutor
from pkgutil import walk_packages
import scipy
def worker():
for _ in walk_packages(scipy.__path__, scipy.__name__ + '.'):
pass
n_threads=10
tpe = ThreadPoolExecutor(max_workers=min((n_threads, 4)))
futures = [None]*n_threads
for i in range(n_threads):
futures[i] = tpe.submit(worker)
[f.result() for f in futures]
This is based on the scipy test scipy/_lib/tests/test_public_api.py::test_all_modules_are_expected running under pytest-run-parallel.
On both the free-threaded and GIL-enabled interpreter, this script eventually fails with the following error:
Traceback (most recent call last):
File "/Users/goldbaum/Documents/test/test.py", line 16, in <module>
[f.result() for f in futures]
~~~~~~~~^^
File "/Users/goldbaum/.pyenv/versions/3.13.4/lib/python3.13/concurrent/futures/_base.py", line 456, in result
return self.__get_result()
~~~~~~~~~~~~~~~~~^^
File "/Users/goldbaum/.pyenv/versions/3.13.4/lib/python3.13/concurrent/futures/_base.py", line 401, in __get_result
raise self._exception
File "/Users/goldbaum/.pyenv/versions/3.13.4/lib/python3.13/concurrent/futures/thread.py", line 59, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/goldbaum/Documents/test/test.py", line 6, in worker
for _ in walk_packages(scipy.__path__, scipy.__name__ + '.'):
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/goldbaum/.pyenv/versions/3.13.4/lib/python3.13/pkgutil.py", line 93, in walk_packages
yield from walk_packages(path, info.name+'.', onerror)
File "/Users/goldbaum/.pyenv/versions/3.13.4/lib/python3.13/pkgutil.py", line 93, in walk_packages
yield from walk_packages(path, info.name+'.', onerror)
File "/Users/goldbaum/.pyenv/versions/3.13.4/lib/python3.13/pkgutil.py", line 93, in walk_packages
yield from walk_packages(path, info.name+'.', onerror)
File "/Users/goldbaum/.pyenv/versions/3.13.4/lib/python3.13/pkgutil.py", line 88, in walk_packages
path = getattr(sys.modules[info.name], '__path__', None) or []
~~~~~~~~~~~^^^^^^^^^^^
KeyError: 'scipy._lib.array_api_compat.dask.array'
It runs successfully if I set n_threads=1 in the script.
I think this is happening because there's a race to call clone_module: https://github.com/data-apis/array-api-compat/blob/6c708d13e826fb850161babf34f306fe80cae875/array_api_compat/_internal.py#L56-L71
- Dominant language
- Python
- Stars
- 131
- Forks
- 49
- Avg merge
- 2d 8h
- Merged PRs (30d)
- 6
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from data-apis/array-api-compat
-
blocked by upstream
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
data-apis/array-api-compat#439 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
data-apis/array-api-compat#395 · 2 comments ·
-
Release 1.16 Open
Difficulty 5/5 Over a week Newbie friendliness 25/100
data-apis/array-api-compat#476 ·
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
data-apis/array-api-compat#473 · 4 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
data-apis/array-api-compat#465 · 3 comments ·
All issues in data-apis/array-api-compat
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
use-agent-os/agent-os#3314 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
BasedHardware/omi#15662 · 1 comment ·
-
documentation help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
AiursoftWeb/AnduinOS-2#19 ·