{"record":{"id":"63fbc92b7539629e","repo":"sgl-project/sglang","slug":"invalid-port-number-s-r","errorCode":null,"errorMessage":"Invalid port number: {s!r}","messagePattern":"Invalid port number: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/utils/network.py","lineNumber":445,"sourceCode":"def _is_ipv6(host: str) -> bool:\n    \"\"\"Check whether *host* is a valid IPv6 address (without brackets).\"\"\"\n    try:\n        ipaddress.IPv6Address(host)\n        return True\n    except ValueError:\n        return False\n\n\ndef _wrap(host: str) -> str:\n    \"\"\"Wrap an IPv6 address in brackets; pass IPv4/hostname through.\"\"\"\n    return f\"[{host}]\" if _is_ipv6(host) else host\n\n\ndef _parse_port(s: str) -> int:\n    try:\n        port = int(s)\n    except ValueError:\n        raise ValueError(f\"Invalid port number: {s!r}\")\n    if not (0 <= port <= 65535):\n        raise ValueError(f\"Port out of range (0-65535): {port}\")\n    return port\n\n\n@dataclass(frozen=True)\nclass NetworkAddress:\n    host: str\n    port: int\n\n    def __post_init__(self):\n        # Auto-strip IPv6 brackets so callers can pass \"[::1]\" or \"::1\"\n        if self.host.startswith(\"[\") and self.host.endswith(\"]\"):\n            object.__setattr__(self, \"host\", self.host[1:-1])\n\n    @property\n    def is_ipv6(self) -> bool:\n        return _is_ipv6(self.host)","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/utils/network.py#L427-L463","documentation":"The NetworkAddress parser's _parse_port converts the port substring to int; a non-numeric token (empty string, hostname fragment, 'auto', trailing colon) raises ValueError with the offending string shown via !r.","triggerScenarios":"NetworkAddress.parse(\"host:abc\"), parse(\"host:\"), or an endpoint string like \"0.0.0.0:auto\" where a non-integer appears where the port must be.","commonSituations":"Config values templated with unset variables producing empty ports; endpoints built by string concatenation where the port piece is optional/None rendered as 'None'.","solutions":["Fix the endpoint string to have a numeric port (host:8000).","Validate env-derived endpoint strings before parsing.","If the port is optional, split and default it before calling parse."],"exampleFix":"# before\naddr = NetworkAddress.parse(f\"{host}:{port_var}\")  # port_var=None -> \"host:None\"\n\n# after\nport_var = port_var or 8000\naddr = NetworkAddress.parse(f\"{host}:{port_var}\")","handlingStrategy":"validation","validationCode":"def valid_port_str(s: str) -> bool:\n    return s.isdigit() and len(s) <= 5","typeGuard":null,"tryCatchPattern":"try:\n    addr = NetworkAddress.parse(endpoint)\nexcept ValueError as e:\n    raise SystemExit(f\"bad endpoint {endpoint!r}: {e}\") from e","preventionTips":["Never f-string None into endpoints; default the port first."],"tags":["network","parsing","address"],"backgroundTag":"invalid-endpoint-string","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}