Make `xml.etree.ElementTree.Element` usable on free-threaded builds
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Crash report
What happened?
Descriptino
_elementtree.c declares Py_MOD_GIL_NOT_USED but has zero @critical_section annotations. element_getitem() checks self->extra != NULL then dereferences self->extra->children[index], but with no lock between the check and the use. Concurrent elem.clear() sets self->extra = NULL and frees the children array between those two operations, causing a NULL dereference.
Repro Code
import sys
import threading
from xml.etree.ElementTree import Element
if sys._is_gil_enabled():
sys.exit("SKIP: requires --disable-gil build")
NUM_THREADS = 4
ITERS = 5_000_000
elem = Element('root')
for i in range(10):
elem.append(Element(f'child{i}'))
def reader():
for _ in range(ITERS):
try:
_ = elem[0]
except IndexError:
pass
def writer():
for _ in range(ITERS):
elem.clear()
for i in range(10):
elem.append(Element(f'child{i}'))
readers = [threading.Thread(target=reader) for _ in range(NUM_THREADS)]
writers = [threading.Thread(target=writer) for _ in range(NUM_THREADS)]
for t in writers + readers:
t.start()
for t in writers + readers:
t.join()
print("Completed without crash.")
Output
[1] 40136 segmentation fault ./python3 /tmp/test_elementtree_race.py
CPython versions tested on:
3.15, CPython main branch
Operating systems tested on:
macOS
Output from running 'python -VV' on the command line:
Python 3.15.0a7+ free-threading build (heads/main:e167e06f8c6, Mar 15 2026, 09:14:39) [Clang 17.0.0 (clang-1700.6.4.2)]
Linked PRs
- gh-157000
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 Modules/_elementtree.c 開始,尤其是 element_getitem(),並在 free-threaded 組建中使用提供的腳本重現 race。查看連結的 PR gh-157000 並執行相關的 ElementTree 測試;當並行存取不再造成當機,且該回歸已受到測試涵蓋時,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- c, python
- 領域
- backend
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 20/100