Skip to content

BUG: np.random.Generator(MyBitGenerator).random() segfaults every time #25023

@LeoVasanko

Description

@LeoVasanko

Describe the issue:

I was trying to implement my own random generator but encountered a crash when trying to use it with a Generator. Mine is based on CFFI so that was a suspect but the simplified test case below crashes any machine where I try it even though it doesn't do anything dangerous.

Reproduce the code example:

import numpy as np

class FakeRandom(np.random.BitGenerator):
    def __init__(self, seed=None):
        super().__init__(seed)

    def random_raw(self, size=None):
        return 0

    def spawn(self, n):
        raise NotImplementedError

bg = FakeRandom()
gen = np.random.Generator(bg)
print(gen)    # Prints Generator(FakeRandom)
gen.random()  # SIGSEGV

Error message:

Traceback:

#0  0x0000000000000000 in ?? ()
#1  0x00007fffa5fa6867 in random_standard_uniform_fill ()
   from /home/user/.local/share/hatch/env/virtual/randquik/45ZJMGs_/randquik/lib/python3.10/site-packages/numpy/random/_generator.cpython-310-x86_64-linux-gnu.so
#2  0x00007fffa61d08d8 in __pyx_f_5numpy_6random_7_common_double_fill ()
   from /home/user/.local/share/hatch/env/virtual/randquik/45ZJMGs_/randquik/lib/python3.10/site-packages/numpy/random/_common.cpython-310-x86_64-linux-gnu.so
#3  0x00007fffa5f805f7 in __pyx_pw_5numpy_6random_10_generator_9Generator_15random ()
   from /home/user/.local/share/hatch/env/virtual/randquik/45ZJMGs_/randquik/lib/python3.10/site-packages/numpy/random/_generator.cpython-310-x86_64-linux-gnu.so
#4  0x00005555556d4dde in ?? ()
#5  0x000055555569cf52 in _PyEval_EvalFrameDefault ()


### Runtime information:

Python 3.10.12, WSL2, Numpy 1.26.1 - SIGSEGV
Python 3.11.0, Windows, Numpy 1.24.1 - just terminates with no error message
Python 3.11.2, Linux, Numpy 1.25.2 - SIGSEGV


### Context for the issue:

My random generator is of better quality and runs faster than MT or PCG64 (which are also very fast in Numpy implementation).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions