Problems with PyGILState_Ensure () and PyGILState_Release ()

M

Mathias Mamsch

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
 
G

Greg Chapman

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?
 
M

Mathias Mamsch

Hi Greg, thanks for your answer ...

I really need help with this, because till now I found no solution to my
problem ... I always get FatalErrors, Deadslocks, or protection faults ...

I created some example code for what I want to accomplish. I have a single
thread which calls some C-Function which should call back into python.

To your questions: no nowhere in *my* code ReleaseThread is called and I
dont write to gstate either ...
I dont understand the thread handling of python completely... Any ideas
where - besides the Python docs - I can find some information about that ?

Thanks Mathias Mamsch

here is some example code which produces my problem.
hope it helps to understand it ...

-----------

#include "python.h"
#include <windows.h>

#include <iostream>

using namespace std;

#define GETLOCK PyGILState_STATE gstate; \
gstate = PyGILState_Ensure ();

#define RELEASELOCK PyGILState_Release (gstate);

static DWORD Tid;

static int CHandleFunction ()
{
GETLOCK
// here will I callback to python ...
cout << "Hello" << endl;
RELEASELOCK

return 0;
}

static PyMethodDef SpamMethods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};

// some C Thread which calls the Handler ...
DWORD __stdcall WCThread (LPVOID lpThreadParameter)
{
while (1)
{
CHandleFunction();
Sleep(500);
}
}

PyMODINIT_FUNC initkdll(void)
{
Py_InitModule("kdll", SpamMethods);
// Create some C++ Thread for testing purposes
CreateThread(NULL,0,WCThread,NULL,0,&Tid);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top