How exactly does that happen? Does it invoke a callback provided by
you? Do you have any control over what and how it executes this code?
C code should never call back to Python without ensuring the GIL, e.g.
using the simplified GIL API. The GIL state need not be known in
advance. That is, it grabs the GIL and then returns the GIL state to
whatever it was before grabbing it. So you have a bug in the C code.
Make sure that whoever writes the C code knows to use the simplified C
API when calling back to Python. If they can use the Python C API to
call back to Python, they should know to ensure the GIL as well.
Sturla
In case you can control the place where that "Python code is caused to be
run", your code may just be missing a "with gil" when entering back into
your own code. If you have a callback function somewhere, you can add this
declaration to its signature to let Cython reacquire the GIL on entry, e.g.
cdef int mycallback(void* context) with gil:
...
Only if your 3rd party C library calls into Python directly (which is
rather unlikely), you're out of luck and must hold the GIL while calling
into it.
Stefan
> In case you can control the place where that "Python code is caused to be
> run", your code may just be missing a "with gil" when entering back into
> your own code. If you have a callback function somewhere, you can add this
> declaration to its signature to let Cython reacquire the GIL on entry, e.g.
>
> cdef int mycallback(void* context) with gil:
Yes.
> Only if your 3rd party C library calls into Python directly (which is
> rather unlikely), you're out of luck and must hold the GIL while calling
> into it.
Or tell who ever wrote it to do this:
{
/* ensure we hold the GIL */
PyGILState_STATE gilstate = PyGILState_Ensure();
/* Use the Python API */
/* restore the state of the GIL */
PyGILState_Release(gilstate);
}
Sturla