From 8c58d355a49abcb97325ea7ed968cfe6f50d113a Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Mon, 16 Feb 2026 10:26:26 -0800 Subject: [PATCH] fix: make aclose() idempotent, fix import ordering --- litellm/litellm_core_utils/streaming_handler.py | 10 ++++++---- litellm/router.py | 3 +-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index e147be892f5..64d3d17fb20 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -158,15 +158,17 @@ class CustomStreamWrapper: async def aclose(self): if self.completion_stream is not None: + stream_to_close = self.completion_stream + self.completion_stream = None # Shield from anyio cancellation so cleanup awaits can complete. # Without this, CancelledError is thrown into every await during # task group cancellation, preventing HTTP connection release. with anyio.CancelScope(shield=True): try: - if hasattr(self.completion_stream, "aclose"): - await self.completion_stream.aclose() - elif hasattr(self.completion_stream, "close"): - result = self.completion_stream.close() + if hasattr(stream_to_close, "aclose"): + await stream_to_close.aclose() + elif hasattr(stream_to_close, "close"): + result = stream_to_close.close() if result is not None: await result except BaseException as e: diff --git a/litellm/router.py b/litellm/router.py index bbd7330c62a..9eeaf8421b7 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -13,8 +13,6 @@ import enum import hashlib import inspect import json - -import anyio import logging import threading import time @@ -35,6 +33,7 @@ from typing import ( cast, ) +import anyio import httpx import openai from openai import AsyncOpenAI