{"record":{"id":"c15ad4ed6037631b","repo":"tokio-rs/tokio","slug":"no-addresses-to-send-data-to","errorCode":null,"errorMessage":"no addresses to send data to","messagePattern":"no addresses to send data to","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"tokio/src/net/udp.rs","lineNumber":1190,"sourceCode":"    /// use tokio::net::UdpSocket;\n    /// use std::io;\n    ///\n    /// #[tokio::main]\n    /// async fn main() -> io::Result<()> {\n    ///     let socket = UdpSocket::bind(\"127.0.0.1:8080\").await?;\n    ///     let len = socket.send_to(b\"hello world\", \"127.0.0.1:8081\").await?;\n    ///\n    ///     println!(\"Sent {} bytes\", len);\n    ///\n    ///     Ok(())\n    /// }\n    /// ```\n    pub async fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], addr: A) -> io::Result<usize> {\n        let mut addrs = to_socket_addrs(addr).await?;\n\n        match addrs.next() {\n            Some(target) => self.send_to_addr(buf, target).await,\n            None => Err(io::Error::new(\n                io::ErrorKind::InvalidInput,\n                \"no addresses to send data to\",\n            )),\n        }\n    }\n\n    /// Attempts to send data on the socket to a given address.\n    ///\n    /// Note that on multiple calls to a `poll_*` method in the send direction, only the\n    /// `Waker` from the `Context` passed to the most recent call will be scheduled to\n    /// receive a wakeup.\n    ///\n    /// # Return value\n    ///\n    /// The function returns:\n    ///\n    /// * `Poll::Pending` if the socket is not ready to write\n    /// * `Poll::Ready(Ok(n))` `n` is the number of bytes sent.","sourceCodeStart":1172,"sourceCodeEnd":1208,"githubUrl":"https://github.com/tokio-rs/tokio/blob/625954f365727668cb02d04172b34f1149637728/tokio/src/net/udp.rs#L1172-L1208","documentation":"UdpSocket::send_to resolves the destination, then calls addrs.next(); on None it returns this InvalidInput error directly (no iteration, no last_err). Unlike the bind/connect variants, send_to only ever tries the first resolved address — so an empty resolution is reported with this distinct message rather than 'could not resolve to any address.'","triggerScenarios":"Calling UdpSocket::send_to(buf, \"host:port\") where to_socket_addrs yields an empty iterator (no A/AAAA records). The match arm None fires immediately.","commonSituations":"Sending a datagram to a misspelled or unresolvable hostname; service-discovery entry with no records; transient DNS failure returning empty; sending to a name with only a CNAME chain that bottoms out.","solutions":["Pre-resolve with tokio::net::lookup_host and require a non-empty result before sending.","Send to a literal SocketAddr when the peer is known by IP.","Treat as 'destination unavailable' and either retry after backoff or drop the datagram per policy.","Cache resolved addresses for hot send loops to avoid re-resolving and to detect resolution regressions early."],"exampleFix":"// before\nlet n = sock.send_to(payload, \"collector.svc:8125\").await?;\n\n// after\nlet target = tokio::net::lookup_host(\"collector.svc:8125\")\n    .await?.next()\n    .ok_or_else(|| anyhow::anyhow!(\"collector.svc unresolved\"))?\n.to_string();\nlet n = sock.send_to(payload, target.parse()?).await?;","handlingStrategy":"validation","validationCode":"let target = tokio::net::lookup_host((host.as_str(), port))\n    .await?.next()\n    .ok_or_else(|| anyhow::anyhow!(\"{host} resolved to no addresses\"))?;\nsock.send_to(buf, target).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 sock.send_to(buf, addr).await {\n    Ok(n) => Ok(n),\n    Err(e) if e.kind() == io::ErrorKind::InvalidInput && e.raw_os_error().is_none() => {\n        // no destination; drop or back off per policy\n        Ok(0)\n    }\n    Err(e) => Err(e.into()),\n}","preventionTips":["Pre-resolve the destination for hot send paths and cache it.","Send to a literal SocketAddr when the peer is known by IP.","Treat empty resolution as 'destination unavailable' and apply backoff or drop policy.","Log unresolved destinations to surface DNS regressions."],"tags":["net","udp","send-to","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-14T05:17:10.506Z"}