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

Aperta
#4,262 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
32.2k
Fork
5.8k
Merge medio
12h 29m
PR unite (30g)
112

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di openai/openai-python

Tutte le issue di openai/openai-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.