callback crashing python

Bryan belred at gmail.com
Fri Oct 20 00:22:29 CEST 2006


Bryan wrote:
> hi,
> 
> i have a multithreaded c server that calls process_method in a different 
> c thread per each call.  process_method calls a python function bar in 
> module foo.  function bar calls back into c.  i've removed all the type 
> error handling and simplified the code to hopefully show a minimum 
> amount of code.  when only one request is hitting the server at a time 
> this works correctly even at fast speeds.  but as soon as a second 
> request is made concurrently, the python24.dll will crash and 
> session.callback() in the python code never returns.  i've tried 
> wrapping the callback code in PyGILState_Ensure(), PyEval_SaveThread() 
> without success.
> 
> does anyone know what i have to do to the c callback to prevent python 
> from crashing?
> 
> thanks,
> 
> bryan
> 
> 
> static void process_method(session *session)
> {
>      PyObject      *py_obj_session = NULL;
>      PyObject      *py_mod_foo     = NULL;
>      PyObject      *py_call_bar    = NULL;
>      PyThreadState *py_interp      = NULL;
> 
>      py_interp = get_py_interpreter(session);
>      PyEval_AcquireLock();
>      PyThreadState_Swap(py_interp);
>      py_obj_session = get_py_session(session);
> 
>      py_mod_foo = PyImport_ImportModule("foo");
>      py_call_bar = PyObject_GetAttrString(py_mod_foo, "bar");
>      PyObject_CallFunctionObjArgs(py_call_bar, py_obj_session, NULL);
> 
>      Py_XDECREF(py_call_bar);
>      Py_XDECREF(py_mod_foo);
>      Py_XDECREF(py_obj_session);
> 
>      PyThreadState_Swap(NULL);
>      PyEval_ReleaseLock();
> }
> 
> 
> # module bar
> 
> def bar(session):
>      session.callback()
> 
> 
> 
> /* session.callback() /*
> static PyObject* callback(PyObject *self, PyObject *args)
> {
>      Py_INCREF(Py_None);
>      return Py_None;
> }
> 

update... it's still crashing even without the callback.


function bar is now changed to something like this:

def bar(session):
     return 1

i'm calling this c method process_method concurrently in two process 
where each process is sending requests at a rate of approximately 100 
per second.  each request is processed on the server side in it's own c 
thread.  when the crash happens, it appears that the python code 
successfully completes.  in other words, it never crashes in the middle 
of function bar.  is PyEval_AquireLock, PyThreadState_Swap thread safe 
across c threads?


thanks,

bryan







More information about the Python-list mailing list