What could be the reason for segmentation fault in python bindings

I want to write python bindings to ac library using cython. The library represents a multithreaded http server and exposes it's API via a header file. Following the cython guide, I wrote a pxd and a pyc file, containing definitions for 3 basic functions:

struct server *server_init()
int server_start(struct server *)
int server_stop(struct server *)

The python wrappers looks similar to:

cdef class Server:
    cdef library.server *_server

    def __cinit__(self):
        self._server = library.server_init()

    def server_start(self):
        return library.server_start(self._server)

    def server_stop(self):
        return library.server_stop(self._server)

The bindings work fine, the server starts and serve content, however, when I stop the server from python, most of the time, the python interpreter crashes with a segmentation fault. The crash happens after the return from c server_stop, and I have no idea what could cause the segfault.

The memory allocation/initialization/free take place in these 3 functions and are handled entirely by the c library, internally.

Maybe I'm missing something, so what could be the reason of the segmentation fault error?

EDIT:

It doesn't reproduce in valgrind nor gdb. However, I generated a core dump, and the backtrace looks like this:

#0  0x00007f4cd0020008 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1
#1  0x00007f4cd00208b3 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1
#2  0x00007f4cd0020c54 in _Unwind_ForcedUnwind () from /lib/x86_64-linux-gnu/libgcc_s.so.1
#3  0x00007f4cd5194630 in __GI___pthread_unwind (buf=<optimized out>) at unwind.c:129
#4  0x00007f4cd518c85b in __do_cancel () at ../nptl/pthreadP.h:264
#5  sigcancel_handler (sig=<optimized out>, si=<optimized out>, ctx=<optimized out>) at nptl-init.c:214
#6  sigcancel_handler (sig=<optimized out>, si=<optimized out>, ctx=<optimized out>) at nptl-init.c:173
#7  <signal handler called>
#8  0x00007f4cd4eba72b in accept4 (fd=<optimized out>, addr=..., addr_len=<optimized out>, flags=<optimized out>) at ../sysdeps/unix/sysv/linux/accept4.c:38
#9  0x00007f4cd3a2ee50 in ?? ()
#10 0x0000000000000000 in ?? ()
链接地址: http://www.djcxy.com/p/43898.html

上一篇: 分配错误分配

下一篇: 在python绑定中可能出现分段错误的原因是什么?