{"record":{"id":"52429914145aa943","repo":"tokio-rs/tokio","slug":"could-not-resolve-to-any-address-524299","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/tcp/stream.rs","lineNumber":131,"sourceCode":"        ///\n        /// The [`write_all`] method is defined on the [`AsyncWriteExt`] trait.\n        ///\n        /// [`write_all`]: fn@crate::io::AsyncWriteExt::write_all\n        /// [`AsyncWriteExt`]: trait@crate::io::AsyncWriteExt\n        pub async fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<TcpStream> {\n            let addrs = to_socket_addrs(addr).await?;\n\n            let mut last_err = None;\n\n            for addr in addrs {\n                match TcpStream::connect_addr(addr).await {\n                    Ok(stream) => return Ok(stream),\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        /// Establishes a connection to the specified `addr`.\n        async fn connect_addr(addr: SocketAddr) -> io::Result<TcpStream> {\n            let sys = mio::net::TcpStream::connect(addr)?;\n            TcpStream::connect_mio(sys).await\n        }\n\n        pub(crate) async fn connect_mio(sys: mio::net::TcpStream) -> io::Result<TcpStream> {\n            let stream = TcpStream::new(sys)?;\n\n            // Once we've connected, wait for the stream to be writable as\n            // that's when the actual connection has been initiated. Once we're\n            // writable we check for `take_socket_error` to see if the connect","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/tokio-rs/tokio/blob/625954f365727668cb02d04172b34f1149637728/tokio/src/net/tcp/stream.rs#L113-L149","documentation":"TcpStream::connect resolves via to_socket_addrs, tries connect_addr on each, and uses unwrap_or_else to build this InvalidInput error only when last_err is None — meaning the resolved address iterator was empty. If candidates existed, the last connect error propagates instead. So this specific message means 'resolution returned no addresses to try.'","triggerScenarios":"Calling TcpStream::connect(\"host:port\") where the hostname resolves to an empty A/AAAA set. The for loop never runs, last_err is None, and the fallback fires.","commonSituations":"Connecting to a typo'd or decommissioned hostname; bare CNAME with no A record; resolver / /etc/hosts returning an empty answer; a stale service-discovery entry pointing at a name with no records.","solutions":["Pre-resolve with tokio::net::lookup_host to confirm addresses exist before connecting.","Use happy-eyeballs / iterate the resolved list yourself to keep going past individual failures.","Distinguish empty-resolution from connect-refused: empty → InvalidInput with no raw os error; refused → raw os error.","Verify DNS externally (dig +short host) and fix or update the resolver."],"exampleFix":"// before\nlet s = TcpStream::connect(\"svc.example:443\").await?;\n\n// after\nlet addrs: Vec<_> = tokio::net::lookup_host(\"svc.example:443\").await?.collect();\nif addrs.is_empty() {\n    return Err(anyhow::anyhow!(\"svc.example resolved to no addresses\"));\n}\nlet mut last = None;\nfor a in addrs {\n    match TcpStream::connect(a).await { Ok(s) => break Ok(s), Err(e) => last = Some(e) }\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 mut last = None;\nfor a in addrs {\n    match TcpStream::connect(a).await { Ok(s) => return Ok(s), Err(e) => last = Some(e) }\n}\nErr(last.unwrap().into())","typeGuard":"fn is_empty_resolution(e: &io::Error) -> bool {\n    e.kind() == io::ErrorKind::InvalidInput && e.raw_os_error().is_none()\n}","tryCatchPattern":"match TcpStream::connect(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":["Pre-resolve and iterate addresses yourself to keep connecting past failures.","Use literal IPs for known peers to avoid DNS ambiguity.","Distinguish empty resolution (no raw os error) from connect-refused (raw os error).","Refresh DNS periodically for long-lived connection pools."],"tags":["net","tcp","connect","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"}