Bug report
Bug description:
event_tstate in Modules/_tkinter.c is written without synchronization in EnableEventHook() (~line 3545) and read under tcl_lock in EventHook() (~line 3507). _tkinter declares Py_MOD_GIL_NOT_USED, so the GIL no longer serializes these accesses under --disable-gil.
A torn read of event_tstate hands ENTER_PYTHON / PyEval_RestoreThread a dangling or NULL thread state.
Code
Write site (no lock):
// Modules/_tkinter.c, EnableEventHook, ~line 3545
event_tstate = tstate;
Read site (under tcl_lock only):
// Modules/_tkinter.c, EventHook, ~line 3507
Py_BEGIN_ALLOW_THREADS
if(tcl_lock) PyThread_acquire_lock(tcl_lock, 1);
tcl_tstate = event_tstate; // racy read
result = Tcl_DoOneEvent(TCL_DONT_WAIT);
tcl_tstate = NULL;
if(tcl_lock) PyThread_release_lock(tcl_lock);
...
Py_END_ALLOW_THREADS
The write in EnableEventHook happens entirely outside tcl_lock.
Suggested fix
// Write site
_Py_atomic_store_ptr(&event_tstate, tstate);
// Read site
tcl_tstate = _Py_atomic_load_ptr(&event_tstate);
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux, macOS
Bug report
Bug description:
event_tstateinModules/_tkinter.cis written without synchronization inEnableEventHook()(~line 3545) and read undertcl_lockinEventHook()(~line 3507)._tkinterdeclaresPy_MOD_GIL_NOT_USED, so the GIL no longer serializes these accesses under--disable-gil.A torn read of
event_tstatehandsENTER_PYTHON/PyEval_RestoreThreada dangling or NULL thread state.Code
Write site (no lock):
Read site (under tcl_lock only):
The write in
EnableEventHookhappens entirely outsidetcl_lock.Suggested fix
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux, macOS