python / python/cpython

It isn't possible to temporarily resurrect objects safely in the Py3.15t limited API

Abierto
#152,406 8 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

interpreter-core topic-C-API type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug report

Bug description:

In Cython we do something like

Py_SET_REFCNT(o, Py_REFCNT(o) + 1);
// Run user implemented deallocation code
Py_SET_REFCNT(o, Py_REFCNT(o) - 1);

The reason for this is that we don't want any refcounting (e.g. of temps) within the user code to accidentally trigger a recursive call to dealloc (the user code really shouldn't resurrect the object and it's their own problem if they do, but it is reasonable for it to generate temporaries, which may well be out of their control anyway).

This turns out not to be thread-safe in the freethreaded build because it makes TryIncRef succeed. Outside the limited API it's possible to fix by manual access to the reference count, but obviously not inside the limited API.

It would be nice to have a way of to avoid this.

  • Most convenient would be to properly access _PyObject_ResurrectStart and _PyObject_ResurrectEnd.
  • The other thing that could work is some way to defer the deallocation until the next GC cycle (when I think any TryIncRefs would have fall out of scope anyway). I suspect I could manually arrange to do that, although it feels fragile, possibly relies on the GC being single-threaded, and which I haven't tested (so may not work...)
CPython versions tested on:

3.15

Operating systems tested on:

No response

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

Empieza revisando la superficie de la limited-API implicada en la resurrección del recuento de referencias y en el comportamiento de TryIncRef en modo free-threaded descrito aquí; después, compárala con _PyObject_ResurrectStart y _PyObject_ResurrectEnd, y con el issue #7769 de Cython. Se considera terminado cuando exista un mecanismo definido y seguro para hilos de la limited-API que evite una resurrección temporal insegura durante el código de desasignación del usuario.

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

Evaluación

Stack tecnológico
c, python
Área
api
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Tranquilo
Claridad
Necesita aclaración
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.