Skip to content

Data race on event_tstate in _tkinter.c under free-threading #153020

Description

@tangyuan0821

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

Metadata

Metadata

Assignees

No one assigned

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions