This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients Mekk, eric.snow, ncoghlan, pitrou, steve.dower, vstinner, xcombelle
Date 2017-12-04.11:51:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
"./Program/_testembed.exe bpo20891" fails randomly on macOS:
macbook:master haypo$ while true; do ./Programs/_testembed bpo20891 ||break; date; done
Lun  4 déc 2017 12:46:34 CET
Lun  4 déc 2017 12:46:34 CET
Lun  4 déc 2017 12:46:34 CET
Fatal Python error: PyEval_SaveThread: NULL tstate

Current thread 0x00007fffa5dff3c0 (most recent call first):
Abort trap: 6

In test_bpo20891() of Program/_testembed.c, Py_BEGIN_ALLOW_THREADS calls PyEval_SaveThread() which fails with a fatal error:

    PyThreadState *tstate = PyThreadState_Swap(NULL);
    if (tstate == NULL)
        Py_FatalError("PyEval_SaveThread: NULL tstate"); <~~~ HERE

I'm not sure that it's safe to create the GIL in PyGILState_Ensure() in a "non-Python" thread, while the main thread (which is a Python thread) "is running".

I found a working fix: call PyEval_InitThreads() in PyThread_start_new_thread(). So the GIL is created as soon as a second thread is spawned. The GIL cannot be created anymore while two threads are running. At least, with the "python" binary. It doesn't fix the issue if a thread is not spawned by Python, but this thread calls PyGILState_Ensure().

Maybe we need to better document how threads have to be initialized to prevent this race condition / bug?
Date User Action Args
2017-12-04 11:51:47vstinnersetrecipients: + vstinner, ncoghlan, pitrou, Mekk, eric.snow, steve.dower, xcombelle
2017-12-04 11:51:47vstinnersetmessageid: <>
2017-12-04 11:51:47vstinnerlinkissue20891 messages
2017-12-04 11:51:46vstinnercreate