Data race on `event_tstate` in `_tkinter.c` under free-threading
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 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