[Bugfix][Frontend] Return 400 for invalid PyNvVideoCodec video input (#51120)

Signed-off-by: Dat Mai <dmai@nvidia.com>
This commit is contained in:
dmai-afk
2026-08-12 10:58:46 +00:00
committed by GitHub
parent e60f3c4c13
commit 86e2ab50aa
3 changed files with 110 additions and 5 deletions
+28
View File
@@ -1,7 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import io
import sys
from pathlib import Path
from types import SimpleNamespace
import numpy as np
import numpy.typing as npt
@@ -19,6 +21,7 @@ from vllm.multimodal.media import ImageMediaIO, VideoMediaIO
from vllm.multimodal.video import (
PYNVVIDEOCODEC_VIDEO_BACKEND,
VIDEO_LOADER_REGISTRY,
PyNvVideoCodecVideoBackend,
VideoLoader,
_pynvvc_frames_to_nhwc,
)
@@ -364,6 +367,31 @@ def test_load_base64_jpeg_raises_on_zero_num_frames():
videoio.load_base64("video/jpeg", data)
def test_pynvvideocodec_unrelated_error_propagates(
monkeypatch: pytest.MonkeyPatch,
):
class FakePyNvVCException(Exception):
pass
fake_nvc = SimpleNamespace(PyNvVCException=FakePyNvVCException)
monkeypatch.setitem(sys.modules, "PyNvVideoCodec", fake_nvc)
original_error = RuntimeError("GPU decoder unavailable")
def raise_unrelated_error(cls, file_path, nvc):
raise original_error
monkeypatch.setattr(
PyNvVideoCodecVideoBackend,
"_read_source_metadata",
classmethod(raise_unrelated_error),
)
with pytest.raises(RuntimeError) as exc_info:
PyNvVideoCodecVideoBackend.decode_frames_pynvvideocodec(b"video", None)
assert exc_info.value is original_error
# ---------------------------------------------------------------------------
# GPU video backend policy tests
# ---------------------------------------------------------------------------
+54
View File
@@ -200,6 +200,60 @@ def test_pynvvideocodec_codec_uses_dynamic_sampling_strategy(
assert metadata["frames_indices"] == [0, 9]
@pytest.mark.skipif(not current_platform.is_cuda(), reason="Requires CUDA")
def test_pynvvideocodec_corrupted_videos_raise_value_error():
valid_video = create_long_gop_video(num_frames=2, width=64, height=64)
corrupted_video = (ASSETS_DIR / "corrupted.mp4").read_bytes()
malformed_video = corrupted_video[:128]
old_slots = PyNvVideoCodecVideoBackend._decoder_slots
old_active_slots = PyNvVideoCodecVideoBackend._active_decoder_slots
old_cond = PyNvVideoCodecVideoBackend._decoder_slot_cond
old_max_slots = PyNvVideoCodecVideoBackend._max_decoder_slots
try:
PyNvVideoCodecVideoBackend._decoder_slots = []
PyNvVideoCodecVideoBackend._active_decoder_slots = 0
PyNvVideoCodecVideoBackend._decoder_slot_cond = threading.Condition()
PyNvVideoCodecVideoBackend._max_decoder_slots = None
loader = VIDEO_LOADER_REGISTRY.load(PYNVVIDEOCODEC_VIDEO_BACKEND)
with pytest.raises(
ValueError,
match=r"^Invalid or unsupported video file\.$",
) as malformed_exc:
loader.load_bytes(
malformed_video,
num_frames=1,
hw_decoders=1,
)
assert malformed_exc.value.__cause__ is not None
with pytest.raises(
ValueError,
match=r"^Invalid or unsupported video file\.$",
) as exc_info:
loader.load_bytes(
corrupted_video,
num_frames=-1,
hw_decoders=1,
)
assert exc_info.value.__cause__ is not None
frames, _ = loader.load_bytes(
valid_video,
num_frames=1,
hw_decoders=1,
)
assert frames.shape[0] == 1
finally:
PyNvVideoCodecVideoBackend._decoder_slots = old_slots
PyNvVideoCodecVideoBackend._active_decoder_slots = old_active_slots
PyNvVideoCodecVideoBackend._decoder_slot_cond = old_cond
PyNvVideoCodecVideoBackend._max_decoder_slots = old_max_slots
@pytest.mark.parametrize("hw_decoders", [1, 3])
def test_pynvvideocodec_decoder_slots_are_bounded(
monkeypatch: pytest.MonkeyPatch,