From 4301be7bdb70a078b5402fc83ee52b209f2256b3 Mon Sep 17 00:00:00 2001 From: metamergebot Date: Sat, 3 Oct 2026 16:00:53 -0700 Subject: [PATCH] Pick the default nccl_port below the kernel ephemeral range (#42405) Co-authored-by: merrymercy --- python/sglang/srt/server_args.py | 9 +++++++-- python/sglang/srt/utils/network.py | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/python/sglang/srt/server_args.py b/python/sglang/srt/server_args.py index 86d90360d2..4c6e03734f 100644 --- a/python/sglang/srt/server_args.py +++ b/python/sglang/srt/server_args.py @@ -50,7 +50,11 @@ from sglang.srt.runtime_context import ( publish, ) from sglang.srt.speculative.decoupled_spec_io import DecoupledSpecIpcConfig -from sglang.srt.utils.network import NetworkAddress, get_free_port, wait_port_available +from sglang.srt.utils.network import ( + NetworkAddress, + get_free_port_below_ephemeral, + wait_port_available, +) logger = logging.getLogger(__name__) @@ -699,7 +703,8 @@ class PortArgs: ) -> PortArgs: cfg = resolving_view(server_args) if server_args.nccl_port is None: - nccl_port = get_free_port() + # The scheduler child binds this later; keep it out of the ephemeral range. + nccl_port = get_free_port_below_ephemeral() else: nccl_port = server_args.nccl_port diff --git a/python/sglang/srt/utils/network.py b/python/sglang/srt/utils/network.py index 5c7803e415..4d443116a8 100644 --- a/python/sglang/srt/utils/network.py +++ b/python/sglang/srt/utils/network.py @@ -3,6 +3,7 @@ from __future__ import annotations import ipaddress import logging import os +import random import socket import time from dataclasses import dataclass @@ -194,6 +195,28 @@ def get_free_port(): return port +def get_free_port_below_ephemeral(low=22000, high=29999, attempts=64): + """Free port under the kernel ephemeral range (32768+ by default), for a + port that a child process binds only later.""" + try: + with open("/proc/sys/net/ipv4/ip_local_port_range") as f: + ephemeral_low = int(f.read().split()[0]) + except (OSError, ValueError, IndexError): + ephemeral_low = 32768 + # bind(0) and connect() only hand out ports from ephemeral_low up. + high = min(high, ephemeral_low - 1) + if high - low + 1 < attempts: + return get_free_port() + for port in random.sample(range(low, high + 1), attempts): + try: + # IPv4 explicitly: the TCPStore binds the IPv4 side of this port. + try_bind_socket("0.0.0.0", port, reuse_addr=False).close() + return port + except OSError: + continue + return get_free_port() + + def bind_port(port): """Bind to a specific port, assuming it's available.""" return try_bind_socket(port=port, listen=True)