Pick the default nccl_port below the kernel ephemeral range (#42405)

Co-authored-by: merrymercy <merrymercy@users.noreply.github.com>
This commit is contained in:
metamergebot
2026-10-03 16:00:53 -07:00
committed by GitHub
co-authored by merrymercy
parent 10edb05e72
commit 4301be7bdb
2 changed files with 30 additions and 2 deletions
+7 -2
View File
@@ -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
+23
View File
@@ -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)