From 448344c0e29383adfe606a5c7ede72dd74705321 Mon Sep 17 00:00:00 2001 From: aoshen02 Date: Fri, 7 Aug 2026 22:39:33 +0800 Subject: [PATCH] [Bugfix] Fix get_open_port() livelock on DP-reserved ports and cover get_open_ports_list (#50965) Signed-off-by: Chenglun Hu Signed-off-by: aoshen02 Co-authored-by: Chenglun Hu Co-authored-by: ferkans-amir Co-authored-by: aoshen02 Co-authored-by: Claude Fable 5 --- tests/utils_/test_network_utils.py | 97 ++++++++++++++++++++++++++++++ vllm/utils/network_utils.py | 26 +++++--- 2 files changed, 115 insertions(+), 8 deletions(-) diff --git a/tests/utils_/test_network_utils.py b/tests/utils_/test_network_utils.py index 157d43cb8f..ae63033003 100644 --- a/tests/utils_/test_network_utils.py +++ b/tests/utils_/test_network_utils.py @@ -1,10 +1,12 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import socket +import threading import pytest import zmq +from vllm.utils import network_utils from vllm.utils.network_utils import ( get_open_port, get_open_ports_list, @@ -17,6 +19,26 @@ from vllm.utils.network_utils import ( ) +def _call_with_timeout(func, timeout: float = 10.0): + """Run func in a daemon thread so a livelock regression fails the test + quickly instead of hanging the suite.""" + result: dict = {} + + def target(): + try: + result["value"] = func() + except BaseException as e: + result["error"] = e + + thread = threading.Thread(target=target, daemon=True) + thread.start() + thread.join(timeout) + assert not thread.is_alive(), f"call did not finish within {timeout}s" + if "error" in result: + raise result["error"] + return result["value"] + + def test_get_open_port(monkeypatch: pytest.MonkeyPatch): with monkeypatch.context() as m: m.setenv("VLLM_PORT", "5678") @@ -29,6 +51,20 @@ def test_get_open_port(monkeypatch: pytest.MonkeyPatch): s3.bind(("localhost", get_open_port())) +def test_get_open_port_vllm_port_in_dp_reserved_range( + monkeypatch: pytest.MonkeyPatch, +): + # VLLM_PORT falling inside the data-parallel reserved window used to make + # get_open_port() loop forever (issue #50024). It must instead return a + # port outside the reserved range. + with monkeypatch.context() as m: + m.setenv("VLLM_DP_MASTER_PORT", "5680") + # 5682 is inside [5680, 5690). + m.setenv("VLLM_PORT", "5682") + port = _call_with_timeout(get_open_port) + assert port not in range(5680, 5690) + + def test_get_open_ports_list_with_vllm_port(monkeypatch: pytest.MonkeyPatch): with monkeypatch.context() as m: m.setenv("VLLM_PORT", "5678") @@ -48,6 +84,67 @@ def test_get_open_ports_list_with_vllm_port(monkeypatch: pytest.MonkeyPatch): s.close() +def test_get_open_port_skips_reserved_dp_master_ports( + monkeypatch: pytest.MonkeyPatch, +): + """VLLM_PORT == VLLM_DP_MASTER_PORT must not livelock by returning the + same free-but-reserved port forever (upstream issue #50024).""" + base = 42123 + with monkeypatch.context() as m: + m.setenv("VLLM_PORT", str(base)) + m.setenv("VLLM_DP_MASTER_PORT", str(base)) + port = _call_with_timeout(get_open_port) + assert port >= base + 10 + + +def test_get_open_ports_list_skips_reserved_dp_master_ports( + monkeypatch: pytest.MonkeyPatch, +): + """The VLLM_PORT band scan must not hand out ports inside the window + reserved for the data parallel master process.""" + base = 42223 + reserved = range(base, base + 10) + with monkeypatch.context() as m: + m.setenv("VLLM_PORT", str(base)) + m.setenv("VLLM_DP_MASTER_PORT", str(base)) + ports = _call_with_timeout(lambda: get_open_ports_list(5)) + assert len(ports) == 5 + assert len(set(ports)) == 5, "ports must be unique" + assert all(p not in reserved for p in ports) + + +def test_get_open_port_ephemeral_skips_reserved_range( + monkeypatch: pytest.MonkeyPatch, +): + """An ephemeral port that happens to fall in the reserved range is + replaced by a rescan starting past the range.""" + base = 42323 + with monkeypatch.context() as m: + m.delenv("VLLM_PORT", raising=False) + m.setenv("VLLM_DP_MASTER_PORT", str(base)) + m.setattr( + network_utils, + "_get_open_port", + lambda start_port=None, max_attempts=None: ( + base if start_port is None else start_port + ), + ) + assert _call_with_timeout(get_open_port) == base + 10 + + +def test_get_open_port_ephemeral_without_dp_master_port( + monkeypatch: pytest.MonkeyPatch, +): + """Without VLLM_PORT and VLLM_DP_MASTER_PORT, an ephemeral port is + returned as before.""" + with monkeypatch.context() as m: + m.delenv("VLLM_PORT", raising=False) + m.delenv("VLLM_DP_MASTER_PORT", raising=False) + port = _call_with_timeout(get_open_port) + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(("localhost", port)) + + @pytest.mark.parametrize( "path,expected", [ diff --git a/vllm/utils/network_utils.py b/vllm/utils/network_utils.py index cf3141a0ce..46f3342363 100644 --- a/vllm/utils/network_utils.py +++ b/vllm/utils/network_utils.py @@ -147,6 +147,14 @@ def get_open_zmq_inproc_path() -> str: return f"inproc://{uuid4()}" +def _get_reserved_port_range() -> range: + """Ports reserved for the data parallel master process (empty if unset).""" + if "VLLM_DP_MASTER_PORT" not in os.environ: + return range(0) + dp_master_port = envs.VLLM_DP_MASTER_PORT + return range(dp_master_port, dp_master_port + 10) + + def get_open_port() -> int: """ Get an open port for the vLLM process to listen on. @@ -156,14 +164,11 @@ def get_open_port() -> int: Right now we reserve 10 ports for the data parallel master process. Currently it uses 2 ports. """ - if "VLLM_DP_MASTER_PORT" in os.environ: - dp_master_port = envs.VLLM_DP_MASTER_PORT - reserved_port_range = range(dp_master_port, dp_master_port + 10) - while True: - candidate_port = _get_open_port() - if candidate_port not in reserved_port_range: - return candidate_port - return _get_open_port() + reserved_port_range = _get_reserved_port_range() + port = _get_open_port() + if port in reserved_port_range: + port = _get_open_port(start_port=reserved_port_range.stop, max_attempts=1000) + return port def get_open_ports_list(count: int = 5) -> list[int]: @@ -174,9 +179,14 @@ def get_open_ports_list(count: int = 5) -> list[int]: """ ports_set = set[int]() if envs.VLLM_PORT is not None: + reserved_port_range = _get_reserved_port_range() next_port = envs.VLLM_PORT for _ in range(count): port = _get_open_port(start_port=next_port, max_attempts=1000) + if port in reserved_port_range: + port = _get_open_port( + start_port=reserved_port_range.stop, max_attempts=1000 + ) ports_set.add(port) next_port = port + 1 return list(ports_set)