From 4b980d9d9c49ee37cd56a0a47df331aea7ada955 Mon Sep 17 00:00:00 2001 From: yryzhan Date: Wed, 20 May 2026 18:47:33 +0200 Subject: [PATCH] fix(streaming): avoid duplicate chunk on cancel race in _put When fut.cancel() returns False the coroutine already completed and the item is in the queue. Previously the loop would continue and enqueue the same item again. Now we return immediately when cancel fails, preventing duplicate chunks under tight timing. --- litellm/litellm_core_utils/streaming_handler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index fcfecf7c438..721287a6f41 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -109,7 +109,8 @@ class _SyncIteratorToQueue: fut.result(timeout=0.5) return except TimeoutError: - fut.cancel() + if not fut.cancel(): + return continue except Exception: return