[pypy-issue] [issue1465] cffi callback crashes in multithread environment

ming tracker at bugs.pypy.org
Wed Apr 24 11:43:59 CEST 2013

ming <humings at gmail.com> added the comment:

I was using cffi callback to call my python function from my c library; it works 
fine when they are all in one thread, but it crashes when my c library create a 
new thread and do the "callback" in new thread.

The error sometime is "Segmentfalut 11", sometimes is "bus error"

Pypy version: pypy-2.0-beta2

CPython works fine in my test program.

You can see the code crashes Pypy in the attached file. The C code is like this:

#include <stdio.h>
int (*global_thread_callback)();

#include <pthread.h>

void *another_thread(void *arg)
	printf("this is another thread\n");
	return NULL;
void register_thread_callback(int (*func)())
	global_thread_callback = func;
	pthread_t tid;

	pthread_create(&tid, NULL, another_thread, NULL);

The Python code is like this:

from cffi import FFI
ffi = FFI()
	void register_thread_callback(int (*func)());

lib = ffi.dlopen("libtest.dylib")

# crashes here
def test_thread_callback():
	print 'thread call back OK'
	return 0

thread_func = ffi.callback("int()", test_thread_callback)


while True:

PyPy bug tracker <tracker at bugs.pypy.org>

More information about the pypy-issue mailing list