sgl-project/sglang · error · ValueError

Port out of range (0-65535): {port}

Error message

Port out of range (0-65535): {port}

What it means

The second stage of _parse_port: the substring parsed to an int successfully but the value is negative or above 65535, which is not a valid TCP port. Raised with the numeric value for easy spotting of off-by-1000 or hex/decimal mistakes.

Source

Thrown at python/sglang/srt/utils/network.py:447

    try:
        ipaddress.IPv6Address(host)
        return True
    except ValueError:
        return False


def _wrap(host: str) -> str:
    """Wrap an IPv6 address in brackets; pass IPv4/hostname through."""
    return f"[{host}]" if _is_ipv6(host) else host


def _parse_port(s: str) -> int:
    try:
        port = int(s)
    except ValueError:
        raise ValueError(f"Invalid port number: {s!r}")
    if not (0 <= port <= 65535):
        raise ValueError(f"Port out of range (0-65535): {port}")
    return port


@dataclass(frozen=True)
class NetworkAddress:
    host: str
    port: int

    def __post_init__(self):
        # Auto-strip IPv6 brackets so callers can pass "[::1]" or "::1"
        if self.host.startswith("[") and self.host.endswith("]"):
            object.__setattr__(self, "host", self.host[1:-1])

    @property
    def is_ipv6(self) -> bool:
        return _is_ipv6(self.host)

    @property

View on GitHub (pinned to 0132848349)

Solutions

  1. Correct the port number to 0-65535.
  2. Validate generated port plans before constructing addresses.
  3. Allocate ports with get_open_port() rather than computing them.

Example fix

# before
NetworkAddress.parse("10.0.0.1:70000")

# after
NetworkAddress.parse("10.0.0.1:8000")
Defensive patterns

Strategy: validation

Validate before calling

port = int(port_str)
assert 0 <= port <= 65535, f"port {port} out of range"

Type guard

def is_tcp_port(v) -> bool:
    return isinstance(v, int) and 0 <= v <= 65535

Prevention

When it happens

Trigger: NetworkAddress.parse("host:70000") or a port computed from config arithmetic exceeding 65535, or a negative value from subtraction.

Common situations: Port offsets added across many data-parallel workers; hex strings parsed as decimal; 0 used as base then decremented.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/312ee55f8c78361f. Report an issue: GitHub.