◐ Shell
clean mode source ↗

Message 97678 - Python tracker

> 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 ;-)