{"record":{"id":"8d1abbf48fe6a1e5","repo":"ultralytics/yolov5","slug":"could-not-resolve-hostname-hostname-e","errorCode":null,"errorMessage":"Could not resolve hostname '{hostname}': {e}","messagePattern":"Could not resolve hostname '(.+?)': (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"models/common.py","lineNumber":823,"sourceCode":"        triton = not any(types) and all([any(s in url.scheme for s in [\"http\", \"grpc\"]), url.netloc])\n        return [*types, triton]\n\n    @staticmethod\n    def _load_metadata(f=Path(\"path/to/meta.yaml\")):\n        \"\"\"Loads metadata from a YAML file, returning strides and names if the file exists, otherwise `None`.\"\"\"\n        if f.exists():\n            d = yaml_load(f)\n            return d[\"stride\"], d[\"names\"]  # assign stride, names\n        return None, None\n\n\ndef _validate_ssrf_url(url: str) -> None:\n    \"\"\"Raise ValueError if url resolves to any private/internal address.\"\"\"\n    hostname = urlparse(url).hostname or \"\"\n    try:\n        results = socket.getaddrinfo(hostname, None)\n    except socket.gaierror as e:\n        raise ValueError(f\"Could not resolve hostname '{hostname}': {e}\") from e\n    for _family, _type, _proto, _canonname, sockaddr in results:\n        addr = ipaddress.ip_address(sockaddr[0])\n        if addr.is_private or addr.is_loopback or addr.is_link_local or addr.is_reserved or addr.is_multicast:\n            raise ValueError(f\"Blocked request to internal address: {addr}\")\n\n\ndef _request_ssrf_url(url: str, max_redirects: int = 5):\n    \"\"\"Fetch a URL after validating each resolved redirect target.\"\"\"\n    session = requests.Session()\n    for _ in range(max_redirects + 1):\n        _validate_ssrf_url(url)\n        response = session.get(url, stream=True, allow_redirects=False)\n        if not response.is_redirect:\n            return response\n        url = urljoin(response.url, response.headers[\"location\"])\n        response.close()\n    raise ValueError(f\"Too many redirects while fetching {url}\")\n","sourceCodeStart":805,"sourceCodeEnd":841,"githubUrl":"https://github.com/ultralytics/yolov5/blob/20d1d78a08277e365d57bfa3a2cce752772d9e59/models/common.py#L805-L841","documentation":"_validate_ssrf_url raises ValueError when socket.getaddrinfo fails for the URL's hostname, i.e. DNS cannot resolve the name at all. This guard runs before every fetch in _request_ssrf_url (including each redirect hop) for remote weight/dataset downloads; a gaierror means the hostname is typo'd, the DNS server is unreachable, or the host genuinely does not exist. The wrapper turns what would be a socket error into ValueError with the hostname spelled out.","triggerScenarios":"attempt_download of a weights URL with a typo'd host (e.g. 'https://githib.com/...'); running fully offline with no DNS; a download URL whose domain expired; a redirect Location header pointing at a dead host.","commonSituations":"Air-gapped or proxy-only environments where DNS for public hosts fails; copy-pasted URLs with transcription errors; container images with broken /etc/resolv.conf.","solutions":["Check the URL string for typos and confirm the host resolves: python -c \"import socket; print(socket.getaddrinfo('example.com', None))\".","If offline, download the file on a connected machine and pass the local path instead of the URL.","Fix container/host DNS (resolv.conf, corporate proxy settings) if unrelated hostnames also fail.","If the failure is on a redirect hop, inspect the server's Location header — the final host may be misspelled or dead."],"exampleFix":"# before\nattempt_download('https://githib.com/ultralytics/yolov5/releases/download/v7.0/yolov5s.pt')\n\n# after\nattempt_download('https://github.com/ultralytics/yolov5/releases/download/v7.0/yolov5s.pt')","handlingStrategy":"validation","validationCode":"import socket\nfrom urllib.parse import urlparse\n\ndef hostname_resolves(url: str) -> bool:\n    host = urlparse(url).hostname or ''\n    try:\n        socket.getaddrinfo(host, None)\n        return True\n    except socket.gaierror:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    attempt_download(url)\nexcept ValueError as e:\n    if 'Could not resolve hostname' in str(e):\n        raise SystemExit(f'Check URL/DNS for {url}') from e","preventionTips":["Resolve download hosts once at job start; fail fast with a clear message.","Keep datasets/weights on stable, well-known domains.","In containers, verify DNS works before long jobs."],"tags":["network","dns","download","ssrf"],"backgroundTag":null,"analyzedSha":"20d1d78a08277e365d57bfa3a2cce752772d9e59","analyzedAt":"2026-08-15T02:56:15.443Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}