python / python/cpython

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

未關閉
#153,020 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

extension-modules topic-free-threading topic-tkinter type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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-153640

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 Modules/_tkinter.c 中的 EnableEventHook (~line 3545) 和 EventHook (~line 3507) 開始,接著檢視建議的原子指標 API。驗證在 --disable-gil 下兩個存取都已同步;完成的標準是移除 event_tstate 的競爭條件,且不引入無效的執行緒狀態處理。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
c, python
領域
backend
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。