[New-bugs-announce] [issue8063] Call _PyGILState_Init() earlier in Py_InitializeEx()

STINNER Victor report at bugs.python.org
Fri Mar 5 00:09:57 CET 2010

New submission from STINNER Victor <victor.stinner at haypocalc.com>:

_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.

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.

files: gil_state_init-trunk.patch
keywords: patch
messages: 100432
nosy: haypo
severity: normal
status: open
title: Call _PyGILState_Init() earlier in Py_InitializeEx()
type: crash
versions: Python 2.7, Python 3.2
Added file: http://bugs.python.org/file16439/gil_state_init-trunk.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list