Skip to content

Commit 8dbcb64

Browse files
committed
fix(streaming): drain iter_bytes() after [DONE] to prevent premature TCP FIN
Fixes openai#3440
1 parent 6d9262d commit 8dbcb64

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

‎src/openai/_streaming.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,17 @@ def __stream__(self) -> Iterator[_T]:
106106
response=response,
107107
)
108108
finally:
109-
# Ensure the response is closed even if the consumer doesn't read all data
109+
# Drain any remaining SSE bytes so that h11 can parse the HTTP/1.1
110+
# chunked-encoding terminator (0\r\n\r\n) and advance to DONE state,
111+
# returning the connection to the pool instead of destroying it with a
112+
# premature TCP FIN. Without this drain, breaking on [DONE] leaves
113+
# iter_bytes() suspended and response.close() takes the destructive path.
114+
# Regression introduced by 6132922c; restored by this change (#3440).
115+
try:
116+
for _ in iterator:
117+
pass
118+
except Exception:
119+
pass
110120
response.close()
111121

112122
def __enter__(self) -> Self:
@@ -216,7 +226,15 @@ async def __stream__(self) -> AsyncIterator[_T]:
216226
response=response,
217227
)
218228
finally:
219-
# Ensure the response is closed even if the consumer doesn't read all data
229+
# Drain any remaining SSE bytes so that h11 can parse the HTTP/1.1
230+
# chunked-encoding terminator (0\r\n\r\n) and advance to DONE state,
231+
# returning the connection to the pool instead of destroying it.
232+
# See #3440 (regression from 6132922c).
233+
try:
234+
async for _ in iterator:
235+
pass
236+
except Exception:
237+
pass
220238
await response.aclose()
221239

222240
async def __aenter__(self) -> Self:

0 commit comments

Comments
 (0)