Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2563 > unrolled thread

PyThreadState_Swap crash

Started byWiktor Adamski <BardzoTajneKonto@interia.pl>
First post2011-04-04 06:08 -0700
Last post2011-04-04 21:15 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  PyThreadState_Swap crash Wiktor Adamski <BardzoTajneKonto@interia.pl> - 2011-04-04 06:08 -0700
    Re: PyThreadState_Swap crash Philip Semanchuk <philip@semanchuk.com> - 2011-04-04 21:15 -0400

#2563 — PyThreadState_Swap crash

FromWiktor Adamski <BardzoTajneKonto@interia.pl>
Date2011-04-04 06:08 -0700
SubjectPyThreadState_Swap crash
Message-ID<2cad664c-f054-47ba-8cb5-8b9073a040af@e8g2000vbz.googlegroups.com>
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().

[toc] | [next] | [standalone]


#2618

FromPhilip Semanchuk <philip@semanchuk.com>
Date2011-04-04 21:15 -0400
Message-ID<mailman.30.1301966127.9059.python-list@python.org>
In reply to#2563
On Apr 4, 2011, at 9:08 AM, Wiktor Adamski wrote:

> 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


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web