STINNER Victor <victor.stin...@gmail.com> added the comment:

"./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?

----------
nosy: +eric.snow, ncoghlan
resolution: fixed -> 
status: closed -> open

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue20891>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to