#10128·numba

Race condition in GUFunc caching can lead to segmentation faults

Author: ArvidJBCreated Jun 25, 2025Updated Sep 24, 2026
Labelsbug - incorrect behavior

Reporting a bug

There's race condition in the caching of GUFuncs. In particular, if the generated function_name.nbc and guf-function_name.nbc end up being generated by different processes it can lead to segmentation faults due to a difference in the native function names being generated between them.

What happens here is a race condition where two processes try to both compile the function simultaneously:

  1. the first process compiles all functions in the file and slow_gufunc is the 2nd function to compile (which means its generated name will contain "abi:v2").
  2. the second process finds cached versions of all other functions, but when it hits slow_gufunc it does not, so it starts compilation and includes "abi:v1" since it's the first function to compile.
  3. the first process finishes compilation and writes numba_segfault_gufuncs.slow_gufunc-13.py311.1.nbc with "v2". It then starts compilation of the GUFunc dispatcher and writes guf-numba_segfault_gufuncs.slow_gufunc-13.py311.1.nbc (with "abi:v2")
  4. the second process finishes compilation and replaces numba_segfault_gufuncs.slow_gufunc-13.py311.1.nbc (with "abi:v1")
  5. the second process then detects that guf-numba_segfault_gufuncs.slow_gufunc-13.py311.1.nbc exists and does nothing for it

Now when you run the process will find and load the "abi:v1" function from numba_segfault_gufuncs.slow_gufunc-13.py311.1.nbc, but then it fails to find "abi:v1" in numba_segfault_gufuncs.slow_gufunc-13.py311.1.nbc and in GUFuncBuilder.build it finds and stores a null function pointer:

        ptr = info.library.get_pointer_to_function(info.name)

which then leads to the segmentation fault.

I can reproduce this when I simulate a long compilation time for the function by stopping the first process in the debugger at the correct time so that the second process also triggers compilation.

(numba_venv) [email protected](nyc):~/numba_gufunc_segfault$ python -m pdb call.py
> /u/bessen/numba_gufunc_segfault/call.py(1)<module>()
-> from numba_segfault_gufuncs import slow_gufunc
(Pdb) import numba.core.caching
(Pdb) b numba.core.caching.IndexDataCacheFile._save_data
Breakpoint 1 at /u/bessen/numba_venv/lib64/python3.11/site-packages/numba/core/caching.py:582
(Pdb) c
(Pdb) c
> /u/bessen/numba_venv/lib64/python3.11/site-packages/numba/core/caching.py(583)_save_data()
-> data = self._dump(data)
(Pdb) p name
'numba_segfault_gufuncs.fast_gufunc-5.py311.1.nbc'
(Pdb) c
> /u/bessen/numba_venv/lib64/python3.11/site-packages/numba/core/caching.py(583)_save_data()
-> data = self._dump(data)
(Pdb) p name
'guf-numba_segfault_gufuncs.fast_gufunc-5.py311.1.nbc'
(Pdb) c
> /u/bessen/numba_venv/lib64/python3.11/site-packages/numba/core/caching.py(583)_save_data()
-> data = self._dump(data)
(Pdb) p name
'numba_segfault_gufuncs.slow_gufunc-13.py311.1.nbc'
(Pdb)
[1]+  Stopped                 python -m pdb call.py
(numba_venv) [email protected](nyc):~/numba_gufunc_segfault$ python -m pdb call.py
> /u/bessen/numba_gufunc_segfault/call.py(1)<module>()
-> from numba_segfault_gufuncs import slow_gufunc
(Pdb) import numba.core.caching
(Pdb) b numba.core.caching.IndexDataCacheFile._save_data
Breakpoint 1 at /u/bessen/numba_venv/lib64/python3.11/site-packages/numba/core/caching.py:582
(Pdb) c
> /u/bessen/numba_venv/lib64/python3.11/site-packages/numba/core/caching.py(583)_save_data()
-> data = self._dump(data)
(Pdb) p name
'numba_segfault_gufuncs.slow_gufunc-13.py311.1.nbc'
(Pdb)
[2]+  Stopped                 python -m pdb call.py
(numba_venv) [email protected](nyc):~/numba_gufunc_segfault$ fg 1
python -m pdb call.py
c
c
> /u/bessen/numba_venv/lib64/python3.11/site-packages/numba/core/caching.py(583)_save_data()
-> data = self._dump(data)
(Pdb) p name
'guf-numba_segfault_gufuncs.slow_gufunc-13.py311.1.nbc'
(Pdb) c
[[1.21496267 1.15277047 1.19729359 ... 1.14870951 1.15396551 1.1126556 ]
 [1.20894633 1.12168833 1.40174758 ... 1.17382645 1.15592255 1.10579022]
 [1.15087623 1.17880768 1.11139756 ... 1.17734436 1.18453585 1.0468635 ]
 ...
 [1.30519866 1.40198762 1.29260139 ... 1.28003219 1.24016101 1.20155218]
 [1.3025131  1.3996865  1.29171117 ... 1.279339   1.23702243 1.19165007]
 [1.28846643 1.38760072 1.27059635 ... 1.2628607  1.18825344 1.1175868 ]]
The program finished and will be restarted
> /u/bessen/numba_gufunc_segfault/call.py(1)<module>()
-> from numba_segfault_gufuncs import slow_gufunc
(Pdb) quit
(numba_venv) [email protected](nyc):~/numba_gufunc_segfault$ fg 2
python -m pdb call.py
c
c
Segmentation fault (core dumped)
(numba_venv) [email protected](nyc):~/numba_gufunc_segfault$ python call.py
Segmentation fault (core dumped)
print(c)(numba_venv) [email protected](nyc):~/numba_gufunc_segfault$ cat numba_segfault_gufuncs.py
import numpy as np
from numba import guvectorize, types


@guvectorize([(types.float64[:], types.float64[:])],
             '(n)->(n)', cache=True)
def fast_gufunc(x, result):
    """Simple GUFunc that compiles quickly"""
    for i in range(x.shape[0]):
        result[i] = x[i] * 2.0


@guvectorize([(types.float64[:, :], types.float64[:, :], types.float64[:, :])],
             '(m,n),(m,n)->(m,n)', cache=True)
def slow_gufunc(a, b, result):
    """Complex GUFunc that compiles slowly due to nested loops and complex operations"""
    m, n = a.shape

    # Make compilation slow with complex nested operations
    for i in range(m):
        for j in range(n):
            temp = 0.0
            # Complex computation that makes compilation slow
            for k in range(min(10, m)):
                for l in range(min(10, n)):
                    if i + k < m and j + l < n:
                        temp += a[i + k, j + l] * b[i + k, j + l]
                        temp *= 1.001  # Small multiplication to add complexity
                        temp = temp ** 1.001  # Power operation
                        temp = np.sin(temp) * np.cos(temp)  # Trigonometric operations
                        temp = np.log(abs(temp) + 1e-10)  # Logarithm
                        temp = np.exp(temp / 10.0)  # Exponential

            # More complex operations
            result[i, j] = temp
            for k in range(3):  # Additional nested loops
                for l in range(3):
                    if i + k < m and j + l < n:
                        result[i, j] += a[i + k, j + l] * b[i + k, j + l]
                        result[i, j] = np.sqrt(abs(result[i, j]) + 1e-10)

(numba_venv) [email protected](nyc):~/numba_gufunc_segfault$ cat call.py
from numba_segfault_gufuncs import slow_gufunc
import numpy as np

a = np.random.rand(100, 100)
b = np.random.rand(100, 100)

c = slow_gufunc(a, b)
print(c)
(numba_venv) [email protected](nyc):~/numba_gufunc_segfault$ python
Python 3.11.8 (main, Sep 17 2024, 14:28:20) [GCC 10.3.1 20210422 (Red Hat 10.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numba
>>> numba.__version__
'0.61.2'
>>>