[issue7544] Fatal error on thread creation in low memory condition

STINNER Victor report at bugs.python.org
Wed Jan 13 02:45:23 CET 2010


STINNER Victor <victor.stinner at haypocalc.com> added the comment:

> Running the tests in debug mode gives the following error:
> ... Fatal Python error: Invalid thread state for this thread

I tried all Lib/test/test_thread*py, but not in debug mode :-/

The problem is here: PyThreadState_New() -> _PyGILState_NoteThreadState() -> PyThread_set_key_value() -> find_key() and find_key() calls PyThread_get_thread_ident(), but the ident is not the right ident :-/

PyThreadState_New() should not call _PyGILState_NoteThreadState(), it should be done _in_ the thread.

I wrote a new patch fixing the unit test. PyThreadState_New() is part of the public API, so I can't change its prototype. And _PyGILState_NoteThreadState() is a private function (use the "static" keyword). Because of that, I introduced two new functions:

 - PyThreadState_Prealloc(): like PyThreadState_New() but for thread preallocation, can be called outside the new state (from another thread)

 - PyThreadState_Init(): have to be called in the new thread to finish the thread state initialization, only required for state created by PyThreadState_Prealloc() (not for PyThreadState_New())

I tried all Lib/test/test_thread*.py tests in debug mode and all test now pass ;-)

----------
Added file: http://bugs.python.org/file15848/thread_prealloc_pystate-2.patch

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


More information about the Python-bugs-list mailing list