{"record":{"id":"7d0d02f5978ee3dc","repo":"tokio-rs/tokio","slug":"invalid-address-family-not-ipv4-or-ipv6","errorCode":null,"errorMessage":"invalid address family (not IPv4 or IPv6)","messagePattern":"invalid address family \\(not IPv4 or IPv6\\)","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"tokio/src/net/tcp/socket.rs","lineNumber":982,"sourceCode":"\n            let raw_fd = std_stream.into_raw_fd();\n            unsafe { TcpSocket::from_raw_fd(raw_fd) }\n        }\n\n        #[cfg(windows)]\n        {\n            use std::os::windows::io::{FromRawSocket, IntoRawSocket};\n\n            let raw_socket = std_stream.into_raw_socket();\n            unsafe { TcpSocket::from_raw_socket(raw_socket) }\n        }\n    }\n}\n\nfn convert_address(address: socket2::SockAddr) -> io::Result<SocketAddr> {\n    match address.as_socket() {\n        Some(address) => Ok(address),\n        None => Err(io::Error::new(\n            io::ErrorKind::InvalidInput,\n            \"invalid address family (not IPv4 or IPv6)\",\n        )),\n    }\n}\n\nimpl fmt::Debug for TcpSocket {\n    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {\n        self.inner.fmt(fmt)\n    }\n}\n\n// These trait implementations can't be build on Windows, so we completely\n// ignore them, even when building documentation.\n#[cfg(any(unix, target_os = \"wasi\"))]\ncfg_unix_or_wasi! {\n    impl AsRawFd for TcpSocket {\n        fn as_raw_fd(&self) -> RawFd {","sourceCodeStart":964,"sourceCodeEnd":1000,"githubUrl":"https://github.com/tokio-rs/tokio/blob/625954f365727668cb02d04172b34f1149637728/tokio/src/net/tcp/socket.rs#L964-L1000","documentation":"convert_address calls socket2::SockAddr::as_socket() and, when it returns None, errors with InvalidInput. as_socket() returns Some only for AF_INET / AF_INET6, so any other address family (Unix, Bluetooth, raw, etc.) falls through. The helper is used internally to normalize local/peer addresses obtained from the OS on a TcpSocket.","triggerScenarios":"Reading local_addr()/peer_addr() (or any path that calls convert_address) on a TcpSocket whose kernel-reported address family is neither IPv4 nor IPv6. Possible with an oddly-constructed fd, an ABI mismatch, or a socket inadvertently created from a non-INET family.","commonSituations":"Passing a non-INET raw fd into TcpSocket::from_raw_fd / from_raw_socket on Windows; a corrupted/mismatched socket2 version; AF that the OS returned is unexpected for a TCP socket. Rare in correct usage — usually indicates a programming or integration error.","solutions":["Ensure the fd handed to TcpSocket was created as SOCK_STREAM over AF_INET/AF_INET6.","Upgrade socket2 to a version matching the tokio build to avoid struct layout drift.","Catch InvalidData here and fall back to a string-formatted address or refuse the connection.","Reproduce with a minimal socket2-only test to isolate whether the OS or the binding is at fault."],"exampleFix":"// before\nlet tcp = unsafe { TcpSocket::from_raw_socket(raw) };\nlet addr = tcp.peer_addr()?; // InvalidInput\n\n// after\nlet tcp = unsafe { TcpSocket::from_raw_socket(raw) };\nlet addr = match tcp.peer_addr() {\n    Ok(a) => a,\n    Err(e) if e.kind() == io::ErrorKind::InvalidInput => {\n        return Err(anyhow::anyhow!(\"non-INET peer addr: {e}\"));\n    }\n    Err(e) => return Err(e.into()),\n};","handlingStrategy":"try-catch","validationCode":"// Confirm the fd is INET before wrapping it:\nuse socket2::Socket;\nlet s = Socket::from(fd);\nif s.local_addr().ok().and_then(|a| a.as_socket()).is_none() {\n    return Err(anyhow::anyhow!(\"fd is not an INET socket\"));\n}","typeGuard":"fn is_non_inet_addr(e: &io::Error) -> bool {\n    e.kind() == io::ErrorKind::InvalidInput\n}","tryCatchPattern":"match tcp.peer_addr() {\n    Ok(a) => Ok(a),\n    Err(e) if e.kind() == io::ErrorKind::InvalidInput => {\n        Err(anyhow::anyhow!(\"non-INET peer address: {e}\"))\n    }\n    Err(e) => Err(e.into()),\n}","preventionTips":["Only feed TcpSocket fds that were created as SOCK_STREAM/AF_INET(6).","Keep tokio and socket2 versions aligned to avoid struct drift.","Reproduce suspected ABI issues in isolation against socket2 directly.","Log the raw family value when you see this error to aid diagnosis."],"tags":["net","tcp","socket","address-family","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"}