Skip to content

fix: close stream generators and cancel embeddings on disconnect#116

Merged
alez007 merged 2 commits into
mainfrom
fix/loader-streaming-cleanup-and-dedup
Jul 5, 2026
Merged

fix: close stream generators and cancel embeddings on disconnect#116
alez007 merged 2 commits into
mainfrom
fix/loader-streaming-cleanup-and-dedup

Conversation

@alez007

@alez007 alez007 commented Jul 5, 2026

Copy link
Copy Markdown
Owner

run_cancellable_stream left the wrapped generator open when the consumer closed the stream early (GeneratorExit), stranding a llama-server --parallel slot or vLLM engine request until GC. Also wraps llama_server's create_embedding in run_cancellable so a disconnected client no longer holds a slot to completion, dedupes _project_embedding_response's usage math into _project_usage, consolidates the duplicated per-loader chat SSE chunk encoder into chat_utils.encode_chat_sse_chunk (switching to model_dump_json for a single serialization pass), and skips the streaming TRACE buffer entirely when TRACE is disabled.

run_cancellable_stream left the wrapped generator open when the
consumer closed the stream early (GeneratorExit), stranding a
llama-server --parallel slot or vLLM engine request until GC. Also
wraps llama_server's create_embedding in run_cancellable so a
disconnected client no longer holds a slot to completion, dedupes
_project_embedding_response's usage math into _project_usage,
consolidates the duplicated per-loader chat SSE chunk encoder into
chat_utils.encode_chat_sse_chunk (switching to model_dump_json for a
single serialization pass), and skips the streaming TRACE buffer
entirely when TRACE is disabled.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request ensures that the underlying asynchronous generator (work) is properly closed when a streaming generator is closed early by the consumer. It also optimizes chat streaming by avoiding unnecessary string buffering when trace logging is disabled, unifies SSE chunk encoding across inference engines, and wraps embedding requests in a cancellable task. A review comment highlights a potential RuntimeError if work.aclose() is called while the background task driving work.__anext__() is still active, suggesting to cancel and await that task first.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread modelship/infer/base_infer.py
run_cancellable_stream's finally block called work.aclose() without
first cancelling next_item when the driving task is torn down while
suspended in asyncio.wait (rather than at the yield) - next_item's
__anext__() call still owns work's frame at that point, so aclose()
raises RuntimeError: aclose(): asynchronous generator is already
running instead of letting CancelledError propagate cleanly.
@alez007

alez007 commented Jul 5, 2026

Copy link
Copy Markdown
Owner Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request enhances the robustness of the asynchronous stream handling in run_cancellable_stream by ensuring that the underlying generator is properly closed on all exit paths and that in-flight tasks are cancelled and awaited during teardown to prevent RuntimeError. It also introduces a shared encode_chat_sse_chunk utility, optimizes trace logging overhead in both llama-server and vLLM inference streams, and adds unit tests to verify early generator closure and task cancellation. There are no review comments, and I have no additional feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@alez007 alez007 merged commit 8404704 into main Jul 5, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant