{"record":{"id":"1dbe33dbdf2a24a1","repo":"tokio-rs/tokio","slug":"connect-cannot-be-called-on-a-datagram-socket","errorCode":null,"errorMessage":"connect cannot be called on a datagram socket","messagePattern":"connect cannot be called on a datagram socket","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"tokio/src/net/unix/socket.rs","lineNumber":203,"sourceCode":"        };\n\n        UnixListener::new(mio)\n    }\n\n    /// Establishes a Unix connection with a peer at the specified socket address.\n    ///\n    /// The `UnixSocket` is consumed. Once the connection is established, a\n    /// connected [`UnixStream`] is returned. If the connection fails, the\n    /// encountered error is returned.\n    ///\n    /// Calling this function on a socket created by [`new_datagram`] will return an error.\n    ///\n    /// This calls the `connect(2)` operating-system function.\n    ///\n    /// [`new_datagram`]: `UnixSocket::new_datagram`\n    pub async fn connect(self, path: impl AsRef<Path>) -> io::Result<UnixStream> {\n        if self.ty() == socket2::Type::DGRAM {\n            return Err(io::Error::new(\n                io::ErrorKind::Other,\n                \"connect cannot be called on a datagram socket\",\n            ));\n        }\n\n        let addr = socket2::SockAddr::unix(path)?;\n        if let Err(err) = self.inner.connect(&addr) {\n            if err.raw_os_error() != Some(libc::EINPROGRESS) {\n                return Err(err);\n            }\n        }\n        let mio = {\n            use std::os::unix::io::{FromRawFd, IntoRawFd};\n\n            let raw_fd = self.inner.into_raw_fd();\n            unsafe { mio::net::UnixStream::from_raw_fd(raw_fd) }\n        };\n","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/tokio-rs/tokio/blob/625954f365727668cb02d04172b34f1149637728/tokio/src/net/unix/socket.rs#L185-L221","documentation":"UnixSocket::connect checks ty(); if it's socket2::Type::DGRAM, it returns io::ErrorKind::Other 'connect cannot be called on a datagram socket' before issuing the kernel connect(2). For DGRAM sockets the connectionless connect semantics differ; tokio reserves connect for stream sockets. The doc states this explicitly for new_datagram-created sockets.","triggerScenarios":"Calling UnixSocket::connect(path) on a socket built with UnixSocket::new_datagram().","commonSituations":"Reusing a stream-client template on a datagram socket; protocol role confusion; refactoring that changed socket type without updating the connect call.","solutions":["Use UnixSocket::new_stream() if you need a connected stream, then call connect().","For datagram exchange, use UnixDatagram::bind / connect (the UnixDatagram API supports a connect-like default-peer) instead of UnixSocket::connect.","Branch on socket type and pick the right client API accordingly.","Add a unit test asserting the socket type matches the call path to catch refactor regressions."],"exampleFix":"// before\nlet s = UnixSocket::new_datagram()?;\ns.connect(\"/tmp/dgram.sock\").await?; // error\n\n// after\nlet s = UnixSocket::new_stream()?;\nlet conn = s.connect(\"/tmp/stream.sock\").await?;","handlingStrategy":"type-guard","validationCode":"let s = UnixSocket::new_stream()?; // for connect semantics\n// If you have an existing fd, check socket2::Type before calling connect.","typeGuard":"fn is_connect_on_datagram(e: &io::Error) -> bool {\n    e.kind() == io::ErrorKind::Other\n        && e.to_string() == \"connect cannot be called on a datagram socket\"\n}","tryCatchPattern":"match sock.connect(path).await {\n    Ok(c) => Ok(c),\n    Err(e) if e.to_string() == \"connect cannot be called on a datagram socket\" => {\n        Err(anyhow::anyhow!(\"use UnixDatagram for datagram sockets\"))\n    }\n    Err(e) => Err(e.into()),\n}","preventionTips":["Use new_stream() when you need connection-oriented connect.","For datagram default-peer, use UnixDatagram::connect, not UnixSocket::connect.","Branch on socket type before choosing the client API.","Unit-test the type/method pairing to catch refactor regressions."],"tags":["unix","socket","datagram","connect","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"}