From 8b91cd5b203598bb06595f3a8cb991fecde94f52 Mon Sep 17 00:00:00 2001 From: Ting SUN Date: Tue, 7 Jul 2026 19:13:41 +0700 Subject: [PATCH] [Bugfix][Core] Close underlying iterator in merge_async_iterators single-iterator fast path (#44726) Signed-off-by: Ting Sun Signed-off-by: Nick Hill Co-authored-by: Nick Hill --- tests/utils_/test_async_utils.py | 22 ++++++++++++++++++++++ vllm/utils/async_utils.py | 11 +++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tests/utils_/test_async_utils.py b/tests/utils_/test_async_utils.py index 03d116bdfd..cd41cdaf26 100644 --- a/tests/utils_/test_async_utils.py +++ b/tests/utils_/test_async_utils.py @@ -40,3 +40,25 @@ async def test_merge_async_iterators(): print("Iterator was cancelled normally") except (Exception, asyncio.CancelledError) as e: raise AssertionError() from e + + +@pytest.mark.asyncio +async def test_merge_async_iterators_single_closes_underlying(): + # The single-iterator fast path must close the underlying generator when + # the merged generator is closed, matching the multi-iterator path. On the + # buggy fast path the underlying generator is left running. + closed = False + + async def gen(): + nonlocal closed + try: + while True: + yield "x" + await asyncio.sleep(0.01) + finally: + closed = True + + merged = merge_async_iterators(gen()) + assert await anext(merged) == (0, "x") + await merged.aclose() + assert closed diff --git a/vllm/utils/async_utils.py b/vllm/utils/async_utils.py index 60c2656975..3faf728384 100644 --- a/vllm/utils/async_utils.py +++ b/vllm/utils/async_utils.py @@ -103,8 +103,15 @@ async def merge_async_iterators( """ if len(iterators) == 1: # Fast-path single iterator case. - async for item in iterators[0]: - yield 0, item + iterator: AsyncGenerator[T, None] | None = iterators[0] + try: + async for item in iterator: # type: ignore[union-attr] + yield 0, item + iterator = None + finally: + if iterator is not None: + with contextlib.suppress(BaseException): + await iterator.aclose() return loop = asyncio.get_running_loop()