Problems with PyGILState_Ensure () and PyGILState_Release ()

Greg Chapman glc at well.com
Sun Aug 15 15:44:13 CEST 2004


On Fri, 13 Aug 2004 13:49:09 +0200, "Mathias Mamsch" <Domoran at yahoo.de> wrote:

>Hi all,
>
>i am writing a python extension in c++.   In the extension a c++ thread
>calls back into python. I did it like this:
>
>---- c++ code ----
>PyGILState_STATE gstate;
>gstate = PyGILState_Ensure ();
>
>... do some work ...
>// call the python callback (Func)
>result = PyEval_CallObject(Func, arglist);
>... so some cleanup ...
>
>PyGILState_Release (gstate);
>return
>-----------------
>
>the callback is executed properly, but the PyGILState_Release gives me the
>following error:
>"Fatal Python error: PyThreadState_Delete: tstate is still current"
>
>What can be wrong here? Any suggestions?
>In my understanding this error should not occur ... Is this a known bug
>maybe? I am using Python 2.4a1 under windows.
>
>Thanks in advance, Mathias Mamsch
>
>

Looking at the code for PyGILState_Release (in pystate.c), it looks like what
must be happening is a disagreement between tcur->gilstate_counter and oldstate
(tcur is the current thread state and oldstate is the parameter passed in).
Specifically, you appear to be passing in something other than
PyGILState_UNLOCKED (so PyEval_ReleaseThread is not called, leaving tcur as
still the current thread), but tcur->gilstate_counter is decremented to 0,
causing the attempt to free tcur.

So it looks like the cause could be either 1) something writing to gstate (in
your code above) between the calls to PyGILState_Ensure and PyGILState_Release,
or 2) something making an extra call to PyGILState_Release (i.e., one not
matched by a call to PyGILState_Ensure).  Could your code be doing either of
those things?

---
Greg Chapman




More information about the Python-list mailing list