Message from discussion
crash when returning from a callback function
Received: by 10.68.232.1 with SMTP id tk1mr690019pbc.7.1348706489937;
Wed, 26 Sep 2012 17:41:29 -0700 (PDT)
X-BeenThere: python-cffi@googlegroups.com
Received: by 10.68.237.161 with SMTP id vd1ls7626038pbc.3.gmail; Wed, 26 Sep
2012 17:41:29 -0700 (PDT)
Received: by 10.68.244.73 with SMTP id xe9mr905154pbc.10.1348706489729;
Wed, 26 Sep 2012 17:41:29 -0700 (PDT)
Date: Wed, 26 Sep 2012 17:41:29 -0700 (PDT)
From: Sarvi Shanmugham <sarvil...@gmail.com>
To: python-cffi@googlegroups.com
Message-Id: <64496124-2568-44da-ad18-22b68d6f8667@googlegroups.com>
In-Reply-To: <6c95bc70-8803-4412-837f-85e905ab0097@googlegroups.com>
References: <6c95bc70-8803-4412-837f-85e905ab0097@googlegroups.com>
Subject: Re: crash when returning from a callback function
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_604_28532091.1348706489435"
------=_Part_604_28532091.1348706489435
Content-Type: multipart/alternative;
boundary="----=_Part_605_12480917.1348706489435"
------=_Part_605_12480917.1348706489435
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Was able to reproduce it
>>> import nova.cffitest
{'dummy_func1': <cdata 'struct test_struct_ *(*)(struct test_struct_ *)'
0xf71f2700>, 'dummy_func2': <function newfunc at 0x8991e2c>,
'spi_iterate_error_code': <cdata 'int(*)(struct tdlhandle_s *,
structtdlhandle_s *, struct spi_error_code_ *, int(*)(struct tdlhandle_s *,
struct tdlhandle_s *, struct tdlhandle_s *, int, void *), void *)'
0xf71f2730>, 'threaded_ballback_test': <cdata 'int(*)(int, int(*)(int,
int))' 0xf71f27a0>}
>>>
>>> def mycallback(x,y):
... print x, y
... return 0
...
>>> x=nova.cffitest.callback('int(*)(int,int)', mycallback)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'callback'
>>> x=nova.cffitest.ffi.callback('int(*)(int,int)', mycallback)
>>> print x
<cdata 'int(*)(int, int)' calling <function mycallback at 0x8991e64>>
>>> nova.cffitest.threaded_ballback_test(1000,x)
0
>>> Beginning Wait ....
Ending Wait ....
10 10
Segmentation fault (core dumped)
[128:~]$
My code library header code
typedef int (*mycallback_func_t)(int, int);
int threaded_ballback_test(int handle, mycallback_func_t mycb);
My library C code
void *my_wait_function( void *ptr )
{
mycallback_func_t cbfunc;
int x;
cbfunc = (mycallback_func_t) ptr;
printf("Beginning Wait .... \n");
sleep(5);
printf("Ending Wait .... \n");
x=cbfunc(10, 10);
printf("Doing Callback Wait .... \n");
return NULL;
}
int threaded_ballback_test(int handle, mycallback_func_t mycb)
{
pthread_t thread;
int x;
x = pthread_create( &thread, NULL, my_wait_function, (void*) mycb);
return 0;
}
Thx,
Sarvi
On Wednesday, September 26, 2012 5:05:57 PM UTC-7, Sarvi Shanmugham wrote:
>
>
> I am working with a multithreaded communication library libmylib.so which
> starts multiple threads to do deal with external communication.
> This library that has been in use for a while by other programs and works
> fine. I am now wrapping it with cffi.
>
> The library needs a C callback function defined as
>
> typedef int (*mylib_client_resp_cb) (int, my_client_resp_t ev,
> my_client_resp_attr_t rattr);
>
> I have defined the callback as following
> @ffi.callback('int(*)(oirlib_handle, my_client_resp_t,
> my_client_resp_attr_t)')
> def my_client_resp_cb(ohandle, ev, rattr):
> print ev
> time.sleep(10)
> print "callback done"
> return 0
>
> The library has an API that needs to register a callback function. I am
> calling it as follows
> mylib.mylib_server_register(server_handle, my_client_resp_cb)
> mylib.mylib_sendevent(event) # This sends out a message and expect the
> response to be available in the callback function
>
> while(True):
> time.sleep(1)
>
> This program crashes the moment I return from the callback function.
> I know because I adjusted the sleep in the callback function
>
> Trouble is, there is no traceback or core. All I see is an exit code of 11
> which I read is SEGV.
> Confirmed by installing a siignal 11 handler in the python program which
> gets called.
>
>
> Question:
> Has the CFFI been tested with wrapping C libraries that are themselves
> multithreaded and doing callbacks
>
> I am gonna try to debug this further an may be try to reproduce it in a
> simpler problem.
> But just wanted to check if this is known problem.
> Thx,
> Sarvi
>
> I am trying to see if this can be reproruced
>
------=_Part_605_12480917.1348706489435
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Was able to reproduce it<div><div>>>> import nova.cffitest</div><d=
iv>{'dummy_func1': <cdata 'struct test_struct_ *(*)(struct test_struct_ =
*)' 0xf71f2700>, 'dummy_func2': <function newfunc at 0x8991e2c>, '=
spi_iterate_error_code': <cdata 'int(*)(struct tdlhandle_s *, structtdlh=
andle_s *, struct spi_error_code_ *, int(*)(struct tdlhandle_s *, struct td=
lhandle_s *, struct tdlhandle_s *, int, void *), void *)' 0xf71f2730>, '=
threaded_ballback_test': <cdata 'int(*)(int, int(*)(int, int))' 0xf71f27=
a0>}</div><div>>>></div><div>>>> def mycallback(x,y):<=
/div><div>... print x, y</div><div>... return 0=
</div><div>...</div><div>>>> x=3Dnova.cffitest.callback('int(*)(in=
t,int)', mycallback)</div><div>Traceback (most recent call last):</div><div=
> File "<stdin>", line 1, in <module></div><div>Attribute=
Error: 'module' object has no attribute 'callback'</div><div>>>> x=
=3Dnova.cffitest.ffi.callback('int(*)(int,int)', mycallback)</div><div>>=
>> print x</div><div><cdata 'int(*)(int, int)' calling <functio=
n mycallback at 0x8991e64>></div><div>>>> nova.cffitest.thre=
aded_ballback_test(1000,x)</div><div>0</div><div>>>> Beginning Wai=
t ....</div><div>Ending Wait ....</div><div>10 10</div><div>Segmentation fa=
ult (core dumped)</div><div>[128:~]$</div><div><br></div><div>My code libra=
ry header code</div><div><br></div><div><div>typedef int (*mycallback_func_=
t)(int, int);</div><div>int threaded_ballback_test(int handle, mycallback_f=
unc_t mycb);</div></div><div><br></div><div>My library C code</div><div><di=
v>void *my_wait_function( void *ptr )</div><div>{</div><div> &=
nbsp;mycallback_func_t cbfunc;</div><div> int x;</div><d=
iv><br></div><div> cbfunc =3D (mycallback_func_t) ptr;</=
div><div> printf("Beginning Wait .... \n");</div><div>&n=
bsp; sleep(5);</div><div> printf("Ending Wa=
it .... \n");</div><div> x=3Dcbfunc(10, 10);</div><div>&=
nbsp; printf("Doing Callback Wait .... \n");</div><div> =
return NULL;</div><div>}</div><div><br></div><div><br></div><d=
iv>int threaded_ballback_test(int handle, mycallback_func_t mycb)</div><div=
>{</div><div> pthread_t thread;</div><div> int x;=
</div><div><br></div><div> x =3D pthread_create( &thread, =
NULL, my_wait_function, (void*) mycb);</div><div><br></div><div> &nbs=
p; return 0;</div><div>}</div></div><div><br></div><div>Thx,</div><div>Sarv=
i</div><br>On Wednesday, September 26, 2012 5:05:57 PM UTC-7, Sarvi Shanmug=
ham wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left:=
0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><br><div>I am workin=
g with a multithreaded communication library libmylib.so which starts multi=
ple threads to do deal with external communication.</div><div>This library =
that has been in use for a while by other programs and works fine. I am now=
wrapping it with cffi.</div><div><br></div><div>The library needs a C call=
back function defined as </div><div><div><br></div><div>typedef int (*=
mylib_client_resp_cb) (int, my_client_resp_t ev, my_client_resp_attr_t ratt=
r);</div></div><div><br></div><div><div>I have defined the callback as foll=
owing</div><div>@ffi.callback('int(*)(oirlib_<wbr>handle, my_client_resp_t,=
my_client_resp_attr_t)')</div><div>def my_client_resp_cb(ohandle, ev, ratt=
r):<br></div><div> print ev</div><div> &nbs=
p;time.sleep(10)</div><div> print "callback done"</div><=
div> return 0</div></div><div><br></div><div>The library=
has an API that needs to register a callback function. I am calling it as =
follows</div><div>mylib.mylib_server_register(<wbr>server_handle, my_client=
_resp_cb)</div><div>mylib.mylib_sendevent(event) # This sends out a message=
and expect the response to be available in the callback function</div><div=
><br></div><div>while(True):</div><div> time.sleep(1)</div><di=
v><br></div><div>This program crashes the moment I return from the callback=
function.</div><div> I know because I adjusted the sleep in the callb=
ack function</div><div><br></div><div>Trouble is, there is no traceback or =
core. All I see is an exit code of 11 which I read is SEGV. </div><div=
>Confirmed by installing a siignal 11 handler in the python program which g=
ets called.</div><div><br></div><div><br></div><div>Question:</div><div>&nb=
sp; Has the CFFI been tested with wrapping C libraries that are them=
selves multithreaded and doing callbacks</div><div><br></div><div>I am gonn=
a try to debug this further an may be try to reproduce it in a simpler prob=
lem. </div><div>But just wanted to check if this is known problem.</di=
v><div>Thx,</div><div>Sarvi </div><div><br></div><div>I am trying to s=
ee if this can be reproruced</div></blockquote></div>
------=_Part_605_12480917.1348706489435--
------=_Part_604_28532091.1348706489435--