473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problems with PyGILState_Ensure () and PyGILState_Release ()

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

Jul 18 '05 #1
2 3683
On Fri, 13 Aug 2004 13:49:09 +0200, "Mathias Mamsch" <Do*****@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

Jul 18 '05 #2
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);
}

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Randall Hopper | last post by:
What is the correct way to propagate exceptions from Python callbacks? When I do this: Python -> C++ -> Python Callback (example attached) an exception raised in the callback doesn't make it...
14
by: Jim Hubbard | last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework? Not if you are unaware of the 1,596 issues listed at KBAlertz (http://www.kbalertz.com/technology_3.aspx). If you are...
1
by: 3f | last post by:
Hello; We have made a web application that people can download from our web site and installed on: Windows XP Windows 2000 Professional Windows 2003 Server Windows 2000 Server
0
by: Ralf Riemer | last post by:
Hi, I have some questions concerning the global interpreter lock: I am working with Windows XP Professional Version 5.1, Python version 2.4.1 and Microsoft Visual C++.Net Version 7.1. >From...
5
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) -...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
1
by: Alexander Eisenhuth | last post by:
Hi, I'm near the ground and need help. I'm building a multithreaded extension with boost.python. python extension ----------------------------------------------------- import extension
0
by: Alexander Eisenhuth | last post by:
Hello, In my mixed C++ / Python Application there are situations where a PyGILState_STATE gil_state = PyGILState_Ensure() produces a deadlock. The background is that a call fro python to cpp...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.