If I put a cffi function in an lru_cache
, I get a segfault, but cannot figure out why.
I am compiling simple C functions at runtime with the cffi
package. I want to cache the results of this compilation because compilation has a non-trivial cost. The most obvious solution is lru_cache
from functools
. However, this causes a segfault when I try to call the function a second time. Why does this happen and what can I do to fix it? One clue is that calling Python's garbage collector between the calls is needed to guarantee the segfault, but I don't understand what is being collected to cause the segfault.
import gc
from functools import lru_cache
from tempfile import TemporaryDirectory
from cffi import FFI
@lru_cache() # Segfault goes away if I get rid of this
def build():
# FFI
ffi = FFI()
ffi.cdef("""
void func(int a);
""")
ffi.set_source('_temp', """
void func(int a) {
return;
}
""")
with TemporaryDirectory() as temp_dir:
lib_path = ffi.compile(tmpdir=temp_dir)
lib = ffi.dlopen(lib_path)
func = lib.func
return func
func = build()
func(1)
gc.collect() # Guarantees segfault
func = build()
func(1)