GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Data race on `event_tstate` in `_tkinter.c` under free-threading · Issue #153020 · python/cpython

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

New issue
Copy link
New issue
Copy link
Open
Open
Data race on event_tstate in _tkinter.c under free-threading#153020
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-free-threadingtopic-tkintertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@tangyuan0821
tangyuan0821
opened on Jul 4, 2026
Issue body actions

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

Linked PRs

  • gh-153020: Fix race condition on event_tstate in _tkinter #153640

Activity

  1. tangyuan0821
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jul 4, 2026
  2. picnixz
    added
    topic-free-threading
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    topic-tkinter
    on Jul 4, 2026
  3. picnixz commented on Jul 4, 2026

    @picnixz
    picnixz
    on Jul 4, 2026
    Member
    More actions

    cc @serhiy-storchaka

  4. tangyuan0821
    added 4 commits that reference this issue on Jul 13, 2026

    pythongh-153020: Fix race condition on event_tstate in _tkinter

    cd8c633

    pythongh-153020: Add NEWS entry for _tkinter data race fix

    6e7c0ef

    pythongh-153020: Fix race condition on event_tstate in _tkinter

    4c998f3

    pythongh-153020: Add NEWS entry for _tkinter data race fix

    00a72c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-free-threadingtopic-tkintertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.