From 2c9d9190713285552d7ea2bb264664c90639c824 Mon Sep 17 00:00:00 2001 From: metamergebot Date: Wed, 21 Oct 2026 01:59:03 -0700 Subject: [PATCH] [Fix] srt: Pin the source address of TCP ZMQ connects for the peer allowlist (#45437) Co-authored-by: Lu Fang <116399278+luccafong@users.noreply.github.com> Co-authored-by: fei-xx --- python/sglang/srt/environ.py | 2 + python/sglang/srt/managers/load_snapshot.py | 8 ++- python/sglang/srt/utils/network.py | 32 ++++++++- test/registered/unit/utils/test_network.py | 78 +++++++++++++++++++++ 4 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 test/registered/unit/utils/test_network.py diff --git a/python/sglang/srt/environ.py b/python/sglang/srt/environ.py index 818e011a61..8b6209e428 100644 --- a/python/sglang/srt/environ.py +++ b/python/sglang/srt/environ.py @@ -368,6 +368,8 @@ class Envs: SGLANG_LOG_PICKLE_IPC_OBJECTS = EnvBool(False) # Comma-separated peer IPs admitted by TCP-bound IPC sockets; empty admits all. SGLANG_ZMQ_ALLOWED_PEERS = EnvTuple(tuple()) + # Source IP for outgoing TCP IPC connects, so an allowlisting peer sees it. + SGLANG_ZMQ_SOURCE_IP = EnvStr(None) SGLANG_USE_MESSAGE_QUEUE_BROADCASTER = EnvBool(True) SGLANG_TCP_STORE_PORT = EnvInt(29600) # Base port hint for ephemeral sockets (ZMQ, SHM broadcaster, etc.). diff --git a/python/sglang/srt/managers/load_snapshot.py b/python/sglang/srt/managers/load_snapshot.py index 01b36f7225..4d65df608a 100644 --- a/python/sglang/srt/managers/load_snapshot.py +++ b/python/sglang/srt/managers/load_snapshot.py @@ -53,7 +53,11 @@ import msgspec.structs from sglang.srt.environ import envs from sglang.srt.runtime_context import get_parallel, get_serving -from sglang.srt.utils.network import is_zmq_endpoint_ipv6, restrict_zmq_peers +from sglang.srt.utils.network import ( + is_zmq_endpoint_ipv6, + restrict_zmq_peers, + zmq_connect, +) logger = logging.getLogger(__name__) @@ -401,7 +405,7 @@ class ZmqLoadSnapshotWriter: self._socket.setsockopt(_zmq.IPV6, 1) self._socket.setsockopt(_zmq.LINGER, 0) self._socket.setsockopt(_zmq.CONFLATE, 1) - self._socket.connect(endpoint) + zmq_connect(self._socket, endpoint) def write(self, snapshot: LoadSnapshot) -> None: if snapshot.dp_rank != self.dp_rank: diff --git a/python/sglang/srt/utils/network.py b/python/sglang/srt/utils/network.py index 04690e7292..11d40c350d 100644 --- a/python/sglang/srt/utils/network.py +++ b/python/sglang/srt/utils/network.py @@ -468,11 +468,41 @@ def get_zmq_socket( restrict_zmq_peers(context, socket) socket.bind(endpoint) else: - socket.connect(endpoint) + zmq_connect(socket, endpoint) return socket +def zmq_connect(socket: zmq.Socket, endpoint: str) -> None: + """Connect `socket`, pinning TCP connects to `SGLANG_ZMQ_SOURCE_IP`. + + `restrict_zmq_peers` admits peers by source address. On a host with + several addresses (e.g. a container on host networking) the kernel may + pick a source outside the allowlist, and a rejected PUSH blocks forever. + The pin uses libzmq's `tcp://;` form, zmq_tcp(7). + """ + socket.connect(_with_source_ip(endpoint)) + + +def _with_source_ip(endpoint: str) -> str: + from sglang.srt.environ import envs + + source = envs.SGLANG_ZMQ_SOURCE_IP.get() + if not source or not endpoint.startswith("tcp://") or ";" in endpoint: + return endpoint + address = endpoint[len("tcp://") :] + host = address.rsplit(":", 1)[0].strip("[]") + try: + source_ip, host_ip = ipaddress.ip_address(source), ipaddress.ip_address(host) + except ValueError: + return endpoint + # A source of the other family cannot reach the host. + if source_ip.version != host_ip.version: + return endpoint + source_host = f"[{source_ip}]" if source_ip.version == 6 else str(source_ip) + return f"tcp://{source_host}:0;{address}" + + def is_zmq_endpoint_ipv6(endpoint: str) -> bool: """Return whether a ZMQ TCP endpoint contains a bracketed IPv6 host.""" prefix = "tcp://[" diff --git a/test/registered/unit/utils/test_network.py b/test/registered/unit/utils/test_network.py new file mode 100644 index 0000000000..72f3150487 --- /dev/null +++ b/test/registered/unit/utils/test_network.py @@ -0,0 +1,78 @@ +"""Unit tests for srt/utils/network.py: ZMQ peer allowlist and source pinning.""" + +import unittest + +import zmq + +from sglang.srt.environ import envs +from sglang.srt.utils import network +from sglang.test.ci.ci_register import register_cpu_ci +from sglang.test.test_utils import CustomTestCase + +register_cpu_ci(est_time=10, suite="base-a-test-cpu") + + +class TestZmqSourceIpEndpoint(CustomTestCase): + def test_rewrites_only_matching_tcp_ip_endpoints(self): + """`SGLANG_ZMQ_SOURCE_IP` is spliced in with libzmq's `src:0;dst` form; + hostnames, ipc endpoints and the other address family pass unchanged.""" + cases = [ + ("10.0.0.5", "tcp://10.0.0.1:5555", "tcp://10.0.0.5:0;10.0.0.1:5555"), + ( + "2001:db8::2", + "tcp://[2001:db8::1]:5555", + "tcp://[2001:db8::2]:0;[2001:db8::1]:5555", + ), + ("2001:db8::2", "tcp://127.0.0.1:5555", "tcp://127.0.0.1:5555"), + ("10.0.0.5", "tcp://somehost:5555", "tcp://somehost:5555"), + ("10.0.0.5", "ipc:///tmp/sock", "ipc:///tmp/sock"), + ( + "10.0.0.5", + "tcp://10.0.0.9:0;10.0.0.1:5555", + "tcp://10.0.0.9:0;10.0.0.1:5555", + ), + (None, "tcp://10.0.0.1:5555", "tcp://10.0.0.1:5555"), + ] + for source, endpoint, expected in cases: + with self.subTest(source=source, endpoint=endpoint): + with envs.SGLANG_ZMQ_SOURCE_IP.override(source): + if source is None: + envs.SGLANG_ZMQ_SOURCE_IP.clear() + self.assertEqual(network._with_source_ip(endpoint), expected) + + +class TestZmqAllowlistAdmitsPinnedSource(CustomTestCase): + """A multi-homed client whose kernel picks a source address outside the + peer allowlist is rejected and its PUSH blocks; pinning the source to the + allowlisted address must get the message through.""" + + def _delivered(self, source: str | None) -> bool: + server_ctx, client_ctx = zmq.Context(), zmq.Context() + try: + with envs.SGLANG_ZMQ_ALLOWED_PEERS.override("127.0.0.2"): + port, server = network.get_zmq_socket(server_ctx, zmq.PULL) + with envs.SGLANG_ZMQ_SOURCE_IP.override(source): + if source is None: + envs.SGLANG_ZMQ_SOURCE_IP.clear() + client = network.get_zmq_socket( + client_ctx, zmq.PUSH, f"tcp://127.0.0.1:{port}", bind=False + ) + client.send(b"ping") + return server.poll(2000) != 0 + finally: + with network._zmq_authenticators_lock: + auth = network._zmq_authenticators.pop(server_ctx.underlying, None) + if auth is not None: + auth.stop() + server_ctx.destroy(linger=0) + client_ctx.destroy(linger=0) + + def test_unpinned_connect_is_rejected(self): + self.assertFalse(self._delivered(None)) + + def test_pinned_connect_is_admitted(self): + self.assertTrue(self._delivered("127.0.0.2")) + + +if __name__ == "__main__": + unittest.main()