AsyncStream does not close its SSE decoder and byte iterators before releasing the response

Abierto
#4,262 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
backend

Línea de trabajo

Start with src/openai/_streaming.py and trace how AsyncStream.__aiter__, _iter_events, close(), and __stream__ manage the decoder, byte iterator, and response. Reproduce the early-break case with the described httpx.AsyncByteStream and MockTransport setup. Done means both inner iterators close before the response when the consumer exits its aclosing block.

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

Descripción

Version: openai 3.46.0 (also main as of 2026-11-09), src/openai/_streaming.py

What happens

AsyncStream.__aiter__ is an async generator that delegates to self._iterator, and
_iter_events iterates self._decoder.aiter_bytes(self.response.aiter_bytes()) without closing
either inner iterator. AsyncStream.close() / __stream__'s cleanup call response.aclose()
only. When a consumer stops early, the decoder generator and the byte iterator are left suspended
and are finalized later by the garbage collector, possibly concurrently with other streams, instead
of being closed before the response they read from is released.

Expected

  • __aiter__ returns self (the class already implements __anext__), removing one generator
    layer.

  • _iter_events closes what it opened:

    body = self.response.aiter_bytes()
    events = self._decoder.aiter_bytes(body)
    try:
        async for sse in events:
            yield sse
    finally:
        try:
            if isinstance(events, AsyncGenerator):
                await events.aclose()
        finally:
            await body.aclose()
    
  • close() and the end of __stream__ close the iterator before the response:

    try:
        await self._iterator.aclose()
    finally:
        await self.response.aclose()
    

How we test it: an httpx.AsyncByteStream whose __aiter__ sets a flag in its finally,
served through MockTransport, consumed through a Responses stream with an early break; with
this change the body and its iterator are both closed when the consumer's aclosing block exits.

We run this as a local patch; happy to open a PR.

Lenguaje dominante
Python
Estrellas
32.2k
Forks
5.8k
Merge medio
12 h 29 min
PR fusionados (30 d)
112

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

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.

Más de openai/openai-python

Todos los issues de openai/openai-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.