Data race in list.sort() on no-gil build
未關閉
還沒有人認領這個 Issue。
interpreter-core
topic-free-threading
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
Summary
As reported in #153852 (TSAN0014), list.sort() writes each slot of the array with a plain, non-atomic store. When another thread concurrently accesses the same list, there is a data race between the in-place sort and the reader, which will be reported by TSAN.
How to reproduce
First we need to enable thread-sanitizer
- ./configure --disable-gil --with-thread-sanitizer
- TSAN_OPTIONS="halt_on_error=1 symbolize=1 history_size=4" ./python repro.py
Then run the following repro.py
import sys, threading
size, rounds, num_threads = 2000, 1500, 32
SCRAMBLED = sorted(range(size), key=lambda x: (x * 2654435761) & 0xFFFFFFFF)
global_list = list(SCRAMBLED)
enter, leave = threading.Barrier(num_threads + 1), threading.Barrier(num_threads + 1)
def reader():
for _ in range(rounds):
enter.wait()
for _x in global_list:
pass
leave.wait()
def main_sorter():
for _ in range(rounds):
global_list[:] = SCRAMBLED
enter.wait()
global_list.sort()
leave.wait()
ts = [threading.Thread(target=reader) for _ in range(num_threads)]
for t in ts:
t.start()
main_sorter()
for t in ts:
t.join()
Finally we could see the log from TSan
WARNING: ThreadSanitizer: data race (pid=3143552)
Write of size 8 at 0xffffb6b3c018 by main thread:
#0 binarysort Objects/listobject.c:1918 (python+0x1c64bc)
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-154572
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先使用列出的 no-GIL 和 ThreadSanitizer 設定執行 repro.py,然後檢查第 1918 行 binarysort 附近的 Objects/listobject.c。將 race 報告與連結的 gh-154572 工作進行比較。當重現不再產生 ThreadSanitizer data-race 報告時,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- c, python
- 領域
- operating-systems, testing-qa
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100