Free threading: Data race on `tool_id` in `sys.monitoring`
未關閉
還沒有人認領這個 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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 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