python / python/cpython

Cross-interpreter memory leak in _xidata_release when originating interpreter is destroyed

Abierto
#153,713 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

interpreter-core topic-subinterpreters 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:

Bug report

Bug summary
There is a known memory leak in Python/crossinterp.c related to the teardown of sub-interpreters when they share data.

When a sub-interpreter packages data into _PyXIData_t and passes it to a receiving interpreter, the receiver is expected to call _PyXIData_Release to free it. However, if the originating interpreter is destroyed before the receiver calls release, the original interpreter's memory context is lost.

In _xidata_release (Python/crossinterp.c around line 1049), the code correctly identifies this state:

    PyInterpreterState *interp = _PyInterpreterState_LookUpID(
                                        _PyXIData_INTERPID(xidata));
    if (interp == NULL) {
        // The interpreter was already destroyed.
        // This function shouldn't have been called.
        // XXX Someone leaked some memory...

Because the originating interpreter is gone, Python intentionally leaks xidata->obj to avoid a use-after-free segfault.

Proposed Solution Architecture
To resolve this without crashing, we would need to track cross-interpreter data at the interpreter level:

  1. Modify PyInterpreterState to include a thread-safe registry tracking all active _PyXIData_t instances created by that interpreter.
  2. Register instances during _PyCode_GetXIData and deregister them during _xidata_release.
  3. Modify Py_EndInterpreter teardown to check this registry. If outstanding shared data exists, the shutting-down interpreter must either block until it is released, or forcefully invalidate/free the shared data (using an is_invalidated flag) before tearing down its own obmalloc state.

I'm opening this issue to get feedback on the proposed architecture before starting a Pull Request.

CPython versions tested on:
main branch

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs
  • gh-153745

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 Python/crossinterp.c alrededor de _xidata_release y, a continuación, sigue _PyCode_GetXIData y la ruta de desmontaje de Py_EndInterpreter. Revisa el trabajo enlazado gh-153745 antes de continuar, ya que este issue solicita comentarios arquitectónicos y no define una implementación establecida ni una prueba de finalización.

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
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.