python / python/cpython

Make `xml.etree.ElementTree.Element` usable on free-threaded builds

未关闭
#146,022 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

extension-modules topic-free-threading topic-XML type-feature
主要语言
Python
星标
77.2k
派生
35.9k
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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 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

把新 issue 发到你的邮箱

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