PyThreadState_Swap crash

W

Wiktor Adamski

I have 2 threads in C code using python 2.5.2. First thread creates
new interpreter (i need several interpreters but those 2 threads use
only one) like that:

PyEval_AcquireLock();
threadState = Py_NewInterpreter();
PyThreadState_Swap(threadState);

// calling python API

PyThreadState_Swap(NULL);
PyEval_ReleaseLock();

Second thread uses interpreter created in first thread:

PyEval_AcquireLock();
PyThreadState_Swap(threadState);

and sometimes PyThreadState_Swap crashes in debug build
(PyGILState_GetThisThreadState() returns garbage). In release build
that code doesn't run and so far no other problem was found.
I call PyEval_InitThreads() at the begining of program and every
PyEval_AcquireLock() has PyEval_ReleaseLock().

Am I allowed to use the same threadState in different threads?
If I am, is there another problem in my code?
Or maybe it's a bug in python - acording to documentation "Python
still supports the creation of additional interpreters (using
Py_NewInterpreter()), but mixing multiple interpreters and the
PyGILState_*() API is unsupported." - I don't use PyGILState_ but it's
used internally in PyThreadState_Swap(). I also don't use
PyEval_RestoreThread() - comment sugests that crashing code is present
because possibility of calling from PyEval_RestoreThread().
 
P

Philip Semanchuk

I have 2 threads in C code using python 2.5.2. First thread creates
new interpreter (i need several interpreters but those 2 threads use
only one) like that:

PyEval_AcquireLock();
threadState = Py_NewInterpreter();
PyThreadState_Swap(threadState);

// calling python API

PyThreadState_Swap(NULL);
PyEval_ReleaseLock();

Second thread uses interpreter created in first thread:

PyEval_AcquireLock();
PyThreadState_Swap(threadState);

and sometimes PyThreadState_Swap crashes in debug build
(PyGILState_GetThisThreadState() returns garbage). In release build
that code doesn't run and so far no other problem was found.
I call PyEval_InitThreads() at the begining of program and every
PyEval_AcquireLock() has PyEval_ReleaseLock().

Am I allowed to use the same threadState in different threads?
If I am, is there another problem in my code?
Or maybe it's a bug in python - acording to documentation "Python
still supports the creation of additional interpreters (using
Py_NewInterpreter()), but mixing multiple interpreters and the
PyGILState_*() API is unsupported." - I don't use PyGILState_ but it's
used internally in PyThreadState_Swap(). I also don't use
PyEval_RestoreThread() - comment sugests that crashing code is present
because possibility of calling from PyEval_RestoreThread().

Hi Wiktor,
I'm sorry I don't have a solution or even a suggestion for you. I just wanted to point out that PyEval_AcquireLock() and PyEval_ReleaseLock() were recently deprecated:
http://bugs.python.org/issue10913

Obviously they'll be around for quite a while longer but given the ominous-but-vague warning in issue10913's description, you might want to stay away from them. It's frustrating for me because I've got code I can't get to work without them.

Good luck
Philip
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top