[issue8063] Call _PyGILState_Init() earlier in Py_InitializeEx()

admin report at bugs.python.org
Wed Mar 10 21:17:38 CET 2010


admin <roundup-admin at psf.upfronthosting.co.za> added the comment:

_PyGILState_Init() initialize autoInterpreterState variable. This variable have to be set before the first call to PyGILState_Ensure().

The problem is that _PyGILState_Init() is called late: at the end of Py_InitializeEx(). It's called after initsite(), whereas initsite() may need to call _PyGILState_Init().

Example: add the following lines at the end of site.py:
---
import readline
import rlcompleter
readline.parse_and_bind("tab: complete")
raw_input("press TAB")
---
Run an interpreter and press TAB:
---
$ ./python
press TABpython: Python/pystate.c:595: PyGILState_Ensure: Assertion `autoInterpreterState' failed.
Abandon
---

Other example of functiions using _PyGILState_Init(): _PyObject_Dump() (useful for debug), sqlite module, os.popen*(), ctypes Python callbacks, etc.

I don't know the right place for _PyGILState_Init() in Py_InitializeEx(). _PyGILState_Init() calls the following functions:
 - PyThread_get_thread_ident()
 - PyThread_allocate_lock()
 - PyThread_acquire_lock()
 - PyThread_release_lock()
 - Py_FatalError() (on error)

None of these function use Python functions, so _PyGILState_Init() can be called very early. The earliest position is just after the call to PyThreadState_New(), before PyThreadState_Swap(). It tested it and it works correctly.

If _PyGILState_Init() is called before PyThreadState_Swap(), PyThreadState_Swap() can be simplified (it doesn't need to check if GIL is initialized or not). Attached patch implement that.

--

I hit this bug when I was debuging Python: I added a call to _PyObject_Dump() which stopped Python (assertion error) because the GIL was not initialized :-/

I already hitted this bug some weeks ago when I was working on the thread state preallocation when creating a new thread: #7544.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8063>
_______________________________________


More information about the Python-bugs-list mailing list