Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix: add cancellation shielding to async_data_generator cleanup per Greptile review

Ryan Crabbe97bc9989435d
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 390d68813e6..9d9734148b7 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -5146,13 +5146,18 @@ async def async_data_generator(
# Close the response stream to release the underlying HTTP connection
# back to the connection pool. This prevents pool exhaustion when
# clients disconnect mid-stream.
- if hasattr(response, "aclose"):
- try:
- await response.aclose()
- except Exception as e:
- verbose_proxy_logger.debug(
- "async_data_generator: error closing response stream: %s", e
- )
+ # Shield from cancellation so the close awaits can complete.
+ import anyio
+
+ with anyio.CancelScope(shield=True):
+ if hasattr(response, "aclose"):
+ try:
+ await response.aclose()
+ except Exception as e:
+ verbose_proxy_logger.debug(
+ "async_data_generator: error closing response stream: %s",
+ e,
+ )
def select_data_generator(