From 006731153693b01320feb93ab165de24fc364b0c Mon Sep 17 00:00:00 2001 From: ErenAta16 Date: Sat, 11 Jul 2026 18:42:08 +0300 Subject: [PATCH] fix(entrypoints): stop resolve_items leaking in-flight media fetch tasks on partial failure (#48333) Signed-off-by: ErenAta16 --- .../entrypoints/unit_tests/test_chat_utils.py | 39 +++++++++++++++++++ vllm/entrypoints/chat_utils.py | 17 ++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/tests/entrypoints/unit_tests/test_chat_utils.py b/tests/entrypoints/unit_tests/test_chat_utils.py index 7738f4c3b0..82b262321d 100644 --- a/tests/entrypoints/unit_tests/test_chat_utils.py +++ b/tests/entrypoints/unit_tests/test_chat_utils.py @@ -1,9 +1,11 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project +import asyncio import warnings from collections.abc import Mapping from typing import Literal +from unittest.mock import MagicMock import pytest import torch @@ -13,6 +15,7 @@ from vllm.assets.image import ImageAsset from vllm.assets.video import VideoAsset from vllm.config import ModelConfig from vllm.entrypoints.chat_utils import ( + AsyncMultiModalItemTracker, ConversationMessage, _postprocess_messages, parse_chat_messages, @@ -2742,3 +2745,39 @@ def test_postprocess_messages_null_arguments_string(): tool_calls = messages[0]["tool_calls"] assert tool_calls is not None assert tool_calls[0]["function"]["arguments"] == {} + + +@pytest.mark.asyncio +async def test_resolve_items_does_not_leak_tasks_on_partial_failure(): + """Regression test: one failing media fetch must not abandon the other + still-in-flight fetches in the same modality batch. + + Before the fix, `resolve_items` gathered per-modality fetches with plain + `asyncio.gather`, so the first exception propagated immediately while + sibling fetches (real network/thread-pool work in production) kept + running detached, with nothing left to await or cancel them. + """ + + async def _fetch(should_fail: bool, delay: float): + if should_fail: + await asyncio.sleep(0.01) + raise ValueError("simulated fetch failure") + await asyncio.sleep(delay) + return ("decoded", None) + + tracker = AsyncMultiModalItemTracker(MagicMock()) + tracker._items_by_modality["image"] = [ + lambda: _fetch(True, 0), + lambda: _fetch(False, 0.2), + lambda: _fetch(False, 0.2), + ] + + tasks_before = asyncio.all_tasks() + with pytest.raises(ValueError, match="simulated fetch failure"): + await tracker.resolve_items() + + leaked_tasks = asyncio.all_tasks() - tasks_before + assert not leaked_tasks, ( + f"resolve_items left {len(leaked_tasks)} task(s) running after " + f"raising: {leaked_tasks}" + ) diff --git a/vllm/entrypoints/chat_utils.py b/vllm/entrypoints/chat_utils.py index f0b56da343..b7d96c894a 100644 --- a/vllm/entrypoints/chat_utils.py +++ b/vllm/entrypoints/chat_utils.py @@ -821,10 +821,19 @@ class AsyncMultiModalItemTracker(BaseMultiModalItemTracker[_AsyncMultiModalItem] if not self._items_by_modality: return None, None - resolved_items_by_modality = { - modality: await asyncio.gather(*(item() for item in items)) - for modality, items in self._items_by_modality.items() - } + resolved_items_by_modality: dict[str, list[Any]] = {} + for modality, items in self._items_by_modality.items(): + results = await asyncio.gather( + *(item() for item in items), return_exceptions=True + ) + for result in results: + if isinstance(result, BaseException): + # Gathering with return_exceptions=True lets every task in + # this modality finish (or itself fail) before we raise, + # instead of abandoning still-in-flight fetches (real + # network/thread-pool work) the moment the first one fails. + raise result + resolved_items_by_modality[modality] = results mm_processor = ( self.mm_processor if self._model_config.is_multimodal_model else None