@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 6b31c84aff
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 15, 09:45 UTC

Throw better error for when running into k8s service discovery issue (#18209)

Signed-off-by: Will Eaton <weaton@redhat.com>

Will Eaton committed May 16, 2025 at 00:07 UTC 6b31c84affbcd2f672915ca2ef5bb39819566441
2 files changed +69 -3
tests/test_vllm_port.py new
+35
@@ -0,0 +1,35 @@
1 +# SPDX-License-Identifier: Apache-2.0
2 +
3 +import os
4 +from unittest.mock import patch
5 +
6 +import pytest
7 +
8 +from vllm.envs import get_vllm_port
9 +
10 +
11 +def test_get_vllm_port_not_set():
12 + """Test when VLLM_PORT is not set."""
13 + with patch.dict(os.environ, {}, clear=True):
14 + assert get_vllm_port() is None
15 +
16 +
17 +def test_get_vllm_port_valid():
18 + """Test when VLLM_PORT is set to a valid integer."""
19 + with patch.dict(os.environ, {"VLLM_PORT": "5678"}, clear=True):
20 + assert get_vllm_port() == 5678
21 +
22 +
23 +def test_get_vllm_port_invalid():
24 + """Test when VLLM_PORT is set to a non-integer value."""
25 + with (patch.dict(os.environ, {"VLLM_PORT": "abc"}, clear=True),
26 + pytest.raises(ValueError, match="must be a valid integer")):
27 + get_vllm_port()
28 +
29 +
30 +def test_get_vllm_port_uri():
31 + """Test when VLLM_PORT is set to a URI."""
32 + with (patch.dict(os.environ, {"VLLM_PORT": "tcp://localhost:5678"},
33 + clear=True),
34 + pytest.raises(ValueError, match="appears to be a URI")):
35 + get_vllm_port()
vllm/envs.py
+34 -3
@@ -139,6 +139,39 @@ def maybe_convert_int(value: Optional[str]) -> Optional[int]:
139 return int(value)
140
141
142 +def get_vllm_port() -> Optional[int]:
143 + """Get the port from VLLM_PORT environment variable.
144 +
145 + Returns:
146 + The port number as an integer if VLLM_PORT is set, None otherwise.
147 +
148 + Raises:
149 + ValueError: If VLLM_PORT is a URI, suggest k8s service discovery issue.
150 + """
151 + if 'VLLM_PORT' not in os.environ:
152 + return None
153 +
154 + port = os.getenv('VLLM_PORT', '0')
155 +
156 + try:
157 + return int(port)
158 + except ValueError as err:
159 + from urllib.parse import urlparse
160 + try:
161 + parsed = urlparse(port)
162 + if parsed.scheme:
163 + raise ValueError(
164 + f"VLLM_PORT '{port}' appears to be a URI. "
165 + "This may be caused by a Kubernetes service discovery issue"
166 + "check the warning in: https://docs.vllm.ai/en/stable/serving/env_vars.html"
167 + )
168 + except Exception:
169 + pass
170 +
171 + raise ValueError(
172 + f"VLLM_PORT '{port}' must be a valid integer") from err
173 +
174 +
175 # The begin-* and end* here are used by the documentation generator
176 # to extract the used env vars.
177
@@ -219,10 +252,8 @@ environment_variables: dict[str, Callable[[], Any]] = {
252 # Note: if VLLM_PORT is set, and some code asks for multiple ports, the
253 # VLLM_PORT will be used as the first port, and the rest will be generated
254 # by incrementing the VLLM_PORT value.
222 - # '0' is used to make mypy happy
255 'VLLM_PORT':
224 - lambda: int(os.getenv('VLLM_PORT', '0'))
225 - if 'VLLM_PORT' in os.environ else None,
256 + get_vllm_port,
257
258 # path used for ipc when the frontend api server is running in
259 # multi-processing mode to communicate with the backend engine process.