fix(entrypoints): stop resolve_items leaking in-flight media fetch tasks on partial failure (#48333)

Signed-off-by: ErenAta16 <erena6466@gmail.com>
This commit is contained in:
ErenAta16
2026-07-11 15:42:08 +00:00
committed by GitHub
parent 51878e5b6e
commit 0067311536
2 changed files with 52 additions and 4 deletions
@@ -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}"
)
+13 -4
View File
@@ -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