python / python/cpython

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

Abierto
#146,022 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

extension-modules topic-free-threading topic-XML type-feature
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con Modules/_elementtree.c, especialmente element_getitem(), y reproduce la condición de carrera usando el script proporcionado en una compilación free-threaded. Revisa el PR vinculado gh-157000 y ejecuta las pruebas relevantes de ElementTree; se considera terminado cuando el acceso concurrente ya no provoca un fallo y la regresión está cubierta.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
c, python
Área
backend
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
20/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.