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

Đang mở
#4,262 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
32.2k
Fork
5.8k
Merge trung bình
12 giờ 29 phút
Pull request đã merge (30 ngày)
112

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của openai/openai-python

Tất cả issue của openai/openai-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.