python / python/cpython

Free threading: Data race on `tool_id` in `sys.monitoring`

未关闭
#154,348 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

interpreter-core topic-free-threading type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

Thanks devdanzin for his fusil, this issue was reported by #153852 TSAN-0030. It;s a data race about interp->monitoring_tool_names[tool_id]

how to reproduce

run the following on a no-gil tsan build

import sys, threading
assert not sys._is_gil_enabled(), "run free-threaded: PYTHON_GIL=0"

# sys.monitoring.use_tool_id(tool_id, name) does an unsynchronized check-then-act on the
# interpreter-global registry interp->monitoring_tool_names[tool_id]:
#     if (interp->monitoring_tool_names[tool_id] != NULL) { raise; }   # :2190  read
#     interp->monitoring_tool_names[tool_id] = Py_NewRef(name);        # :2194  write
# Many threads racing to claim the SAME free tool id read the slot while one writes it.
mon = sys.monitoring
TOOL_ID = 3                 # any 0..5; not reserved
NT = 8
ROUNDS = 6000

enter = threading.Barrier(NT + 1)
leave = threading.Barrier(NT + 1)

def worker():
    for _ in range(ROUNDS):
        enter.wait()            # all workers released together onto a freshly-freed slot
        try:
            mon.use_tool_id(TOOL_ID, "t")   # read :2190 races the winner's write :2194
        except ValueError:
            pass                # "tool 3 is already in use" -> lost the race, expected
        leave.wait()

ts = [threading.Thread(target=worker, name=f"w{i}") for i in range(NT)]
for t in ts:
    t.start()
for r in range(ROUNDS):
    mon.free_tool_id(TOOL_ID)   # NULL the slot so the next round starts from a free id
    enter.wait()
    leave.wait()
for t in ts:
    t.join()
print("done, no crash")
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-154459

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从第 2190 行和第 2194 行附近引用的入口点 sys.monitoring.use_tool_id() 和 free_tool_id() 开始,然后在 no-GIL TSAN 构建上运行提供的 reproducer。完成的标准是:重复的并发 claim/free 测试不再报告 data race,同时保留预期的 tool-id 行为;链接的 PR gh-154459 表明相关工作已经在进行中。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。