Skip to content

fix: consume stream to prevent hanging#260

Merged
gradenr merged 2 commits into
groq:nextfrom
dm-cohere:fix/consume-stream-to-prevent-hanging
Dec 4, 2025
Merged

fix: consume stream to prevent hanging#260
gradenr merged 2 commits into
groq:nextfrom
dm-cohere:fix/consume-stream-to-prevent-hanging

Conversation

@dm-cohere

@dm-cohere dm-cohere commented Nov 28, 2025

Copy link
Copy Markdown
Contributor

This commit changed how the stream was closed, and means that clients can end up hanging after calling the stream.


Note

Ensure sync/async streaming drains remaining SSE events after [DONE] so the HTTP response closes without hanging.

  • Streaming:
    • Update Stream.__stream__ and AsyncStream.__stream__ in src/groq/_streaming.py:
      • Remove try/finally immediate response close; instead drain the SSE iterator after encountering [DONE] to trigger connection closure.
      • Preserve error handling and data processing logic for normal and error events.

Written by Cursor Bugbot for commit 8ac6a76. This will update automatically on new commits. Configure here.

@gradenr

gradenr commented Dec 3, 2025

Copy link
Copy Markdown
Collaborator

This issue has been in the code generator.

@gradenr gradenr closed this Dec 3, 2025
@gradenr gradenr reopened this Dec 4, 2025
@gradenr gradenr changed the base branch from main to next December 4, 2025 18:03
@gradenr gradenr merged commit 42fcb07 into groq:next Dec 4, 2025
7 checks passed
@stainless-app stainless-app Bot mentioned this pull request Dec 4, 2025
Comment thread src/groq/_streaming.py
yield process_data(data={"data": data, "event": sse.event}, cast_to=cast_to, response=response)
# The stream needs to be fully consumed to close the response
for _sse in iterator:
...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Stream not consumed when exception occurs during streaming

The removal of the try-finally block creates a bug where the stream-consuming code at the end of __stream__ (lines 98-100 for sync, 199-201 for async) is never reached if an APIError is raised during iteration. When an error SSE is received and APIError is raised, the exception propagates immediately without consuming the remaining stream, which could cause the same hanging issue this PR attempts to fix. The stream consumption logic needs to be wrapped in a try-finally to ensure it executes even when exceptions occur.

Additional Locations (1)

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants