{"record":{"id":"291544047b24f9ec","repo":"tokio-rs/tokio","slug":"could-not-resolve-to-any-address-291544","errorCode":null,"errorMessage":"could not resolve to any address","messagePattern":"could not resolve to any address","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"tokio/src/net/udp.rs","lineNumber":162,"sourceCode":"    ///     let sock = UdpSocket::bind(\"0.0.0.0:8080\").await?;\n    ///     // use `sock`\n    /// #   let _ = sock;\n    ///     Ok(())\n    /// }\n    /// ```\n    pub async fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> {\n        let addrs = to_socket_addrs(addr).await?;\n        let mut last_err = None;\n\n        for addr in addrs {\n            match UdpSocket::bind_addr(addr) {\n                Ok(socket) => return Ok(socket),\n                Err(e) => last_err = Some(e),\n            }\n        }\n\n        Err(last_err.unwrap_or_else(|| {\n            io::Error::new(\n                io::ErrorKind::InvalidInput,\n                \"could not resolve to any address\",\n            )\n        }))\n    }\n\n    fn bind_addr(addr: SocketAddr) -> io::Result<UdpSocket> {\n        let sys = mio::net::UdpSocket::bind(addr)?;\n        UdpSocket::new(sys)\n    }\n\n    #[track_caller]\n    fn new(socket: mio::net::UdpSocket) -> io::Result<UdpSocket> {\n        let io = PollEvented::new(socket)?;\n        Ok(UdpSocket { io })\n    }\n\n    /// Creates new `UdpSocket` from a previously bound `std::net::UdpSocket`.","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/tokio-rs/tokio/blob/625954f365727668cb02d04172b34f1149637728/tokio/src/net/udp.rs#L144-L180","documentation":"UdpSocket::bind follows the same pattern as the TCP variants: resolve, iterate, fall back to this InvalidInput error only when last_err is None. That branch is reached exclusively when to_socket_addrs returned an empty iterator — no addresses to attempt a bind against.","triggerScenarios":"Calling UdpSocket::bind(\"host:port\") where the hostname resolves to zero A/AAAA records, so the for loop body never executes and last_err stays None.","commonSituations":"Bad hostname; service with no A record; misconfigured DNS or /etc/hosts; binding by name to a host that no longer exists; passing a name where a literal IP is intended.","solutions":["Bind to a literal IP (e.g. \"0.0.0.0:port\" or \"127.0.0.1:port\") — the recommended idiom for UDP listeners.","Pre-resolve with tokio::net::lookup_host and assert non-empty before binding.","Inspect the error: InvalidInput with no raw_os_error means empty resolution; otherwise a real bind error.","Validate the hostname with an external resolver (dig/nslookup)."],"exampleFix":"// before\nlet s = UdpSocket::bind(\"my-svc:9090\").await?;\n\n// after\nlet s = UdpSocket::bind((\"0.0.0.0\", 9090)).await?;\n// or check resolution explicitly:\nif tokio::net::lookup_host(\"my-svc:9090\").await?.next().is_none() {\n    return Err(anyhow::anyhow!(\"no addresses for my-svc\"));\n}","handlingStrategy":"validation","validationCode":"let addrs: Vec<_> = tokio::net::lookup_host((host.as_str(), port)).await?.collect();\nif addrs.is_empty() {\n    return Err(anyhow::anyhow!(\"{host} resolved to no addresses\"));\n}\nlet s = UdpSocket::bind(addrs[0]).await?;","typeGuard":"fn is_empty_resolution(e: &io::Error) -> bool {\n    e.kind() == io::ErrorKind::InvalidInput && e.raw_os_error().is_none()\n}","tryCatchPattern":"match UdpSocket::bind(addr).await {\n    Ok(s) => Ok(s),\n    Err(e) if e.kind() == io::ErrorKind::InvalidInput && e.raw_os_error().is_none() => {\n        Err(anyhow::anyhow!(\"no addresses resolved for {addr}\"))\n    }\n    Err(e) => Err(e.into()),\n}","preventionTips":["Bind UDP listeners to literal IPs, not hostnames.","Pre-resolve and confirm non-empty before binding.","Distinguish empty resolution from real bind errors by checking raw_os_error.","Validate hostnames at config load time."],"tags":["net","udp","bind","dns","tokio"],"backgroundTag":null,"analyzedSha":"625954f365727668cb02d04172b34f1149637728","analyzedAt":"2026-08-11T17:46:45.378Z","contentChangedAt":"2026-08-11T17:46:45.378Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}