Author steve.dower
Recipients pitrou, steve.dower, vstinner
Date 2014-03-11.18:40:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1394563213.18.0.508100602451.issue20891@psf.upfronthosting.co.za>
In-reply-to
Content
In Python 3.4rc3, calling PyGILState_Ensure() from a thread that was not created by Python and without any calls to PyEval_InitThreads() will cause a fatal exit:

Fatal Python error: take_gil: NULL tstate

I believe this was added in http://hg.python.org/cpython/rev/dc4e805ec68a. The attached file has a minimal repro (for Windows, sorry, but someone familiar with low-level *nix threading APIs should be able to translate it easily).

Basically, the sequence looks like this:

1. main() calls Py_Initialize()
2. main() starts new C-level thread
3. main() calls Py_BEGIN_ALLOW_THREADS and waits for the new thread
4. New thread calls PyGILState_Ensure()
5. PyGILState_Ensure() sees that there is no thread state and calls PyEval_InitThreads() (this is what changed in the linked changeset)
6. PyEval_InitThreads() creates the GIL
7. PyEval_InitThreads() calls take_gil(PyThreadState_GET()) (this is what aborts - there is still no thread state)
8. (expected) PyGILState_Ensure() calls PyThreadState_New() and PyEval_RestoreThread().

The attached repro has two workarounds (commented out). The first is to call PyThreadState_New()/PyEval_RestoreThread() from the new thread, and the second is to call PyEval_InitThreads() from the main thread.

The latter is inappropriate for my use, since I am writing a debugger that can inject itself into a running CPython process - there is no way to require that PyEval_InitThreads() has been called and I can't reliably inject it onto the main thread. The first workaround is okay, but I'd rather not have to special-case 3.4 by rewriting PyGILState_Ensure() so that I can safely call PyGILState_Ensure().
History
Date User Action Args
2014-03-11 18:40:13steve.dowersetrecipients: + steve.dower, pitrou, vstinner
2014-03-11 18:40:13steve.dowersetmessageid: <1394563213.18.0.508100602451.issue20891@psf.upfronthosting.co.za>
2014-03-11 18:40:13steve.dowerlinkissue20891 messages
2014-03-11 18:40:12steve.dowercreate