[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 <fei-xx@users.noreply.github.com>
This commit is contained in:
metamergebot
2026-10-21 01:59:03 -07:00
committed by GitHub
co-authored by Lu Fang fei-xx
parent 21f775cd97
commit 2c9d919071
4 changed files with 117 additions and 3 deletions
+2
View File
@@ -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.).
+6 -2
View File
@@ -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:
+31 -1
View File
@@ -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://<source_endpoint>;<endpoint>` 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://["