Disk cache crashes for exec()'d functions with `__module__=None`
Author: ricardoV94Created Mar 18, 2026Updated Sep 14, 2026
Labelsgood first issuebug - incorrect behavior
We found this in PyTensor, when running asv benchmark and trying to debug another issue that actually affects related to caching and os.fork.
"""[debug.py]
Run twice. First run compiles and caches. Second run loads cache and crashes.
$ python debug.py # first: compiles and caches — OK
$ python debug.py # second: loads cache — CRASH
Clean up with: rm -rf _numba_bug_cache
"""
import os, numba, numpy as np
cache_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "_numba_bug_cache")
src_file = os.path.join(cache_dir, "wrapper.py")
# Only create the file if it doesn't exist (preserve mtime for cache validity)
if not os.path.exists(src_file):
os.makedirs(cache_dir, exist_ok=True)
with open(src_file, "w") as fh:
fh.write("def wrapper(*args): return _inner(*args)\n")
@numba.njit
def add_one(x):
return x + 1
env = {"_inner": add_one}
# needs a __name__
exec(compile(open(src_file).read(), src_file, "exec"), env)
f = numba.njit(cache=True)(env["wrapper"])
print(f(np.array([1.0, 2.0, 3.0])))Traceback (most recent call last):
File "/home/ricardo/Documents/pytensor-tree1/debug_numba_fork.py", line 28, in <module>
print(f(np.array([1.0, 2.0, 3.0])))
~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ricardo/Documents/pytensor-tree1/.venv/lib64/python3.13/site-packages/numba/core/dispatcher.py", line 443, in _compile_for_args
raise e
File "/home/ricardo/Documents/pytensor-tree1/.venv/lib64/python3.13/site-packages/numba/core/dispatcher.py", line 376, in _compile_for_args
return_val = self.compile(tuple(argtypes))
File "/home/ricardo/Documents/pytensor-tree1/.venv/lib64/python3.13/site-packages/numba/core/dispatcher.py", line 889, in compile
cres = self._cache.load_overload(sig, self.targetctx)
File "/home/ricardo/Documents/pytensor-tree1/.venv/lib64/python3.13/site-packages/numba/core/caching.py", line 721, in load_overload
return self._load_overload(sig, target_context)
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "/home/ricardo/Documents/pytensor-tree1/.venv/lib64/python3.13/site-packages/numba/core/caching.py", line 728, in _load_overload
data = self._cache_file.load(key)
File "/home/ricardo/Documents/pytensor-tree1/.venv/lib64/python3.13/site-packages/numba/core/caching.py", line 576, in load
return self._load_data(data_name)
~~~~~~~~~~~~~~~^^^^^^^^^^^
File "/home/ricardo/Documents/pytensor-tree1/.venv/lib64/python3.13/site-packages/numba/core/caching.py", line 618, in _load_data
tup = pickle.loads(data)
File "/home/ricardo/Documents/pytensor-tree1/.venv/lib64/python3.13/site-packages/numba/core/environment.py", line 51, in _rebuild_env
mod = importlib.import_module(modname)
File "/usr/lib64/python3.13/importlib/__init__.py", line 80, in import_module
if name.startswith('.'):
^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'startswith'Source: numba/numba