Fatal Python error: PyThreadState_Get: no current thread - Segmentation fault: 11
I am trying to run a tiny CFFI example in which I create and load a dynamic library. However, when I try to execute a function from the dynamic library, it fails. I am running in on MacOS 10.15.7 with Python 3.6.0 and cffi 1.14.5. I tried creating the shared library using C# and it works. So the problem is the creation of the dynamic library in Python.
This is the error message:
Fatal Python error: PyThreadState_Get: no current thread
Current thread 0x00000001132d2dc0 (most recent call first):
File "client.py", line 15 in <module>
Abort trap: 6
protocol.h
looks like this:
#include <stdio.h>
void fooz(int a);
backend.py
:
import cffi
ffibuilder = cffi.FFI()
with open('protocol.h') as f:
data = ''.join([line for line in f if not line.startswith('#')])
ffibuilder.embedding_api(data)
ffibuilder.set_source("lang_protocol", r'''
#include "protocol.h"
''')
ffibuilder.embedding_init_code("""
from lang_protocol import ffi
@ffi.def_extern()
def fooz(c):
a = 1+1
return
""")
ffibuilder.compile(verbose=True)
client.py
:
from cffi import FFI
import os
ffi = FFI()
ffi.cdef("""
int fooz(int a);
""")
lib = ffi.dlopen("lang_protocol.dylib")
print(lib.fooz(6))