{"record":{"id":"5a91c5a56e7f2b46","repo":"tokio-rs/tokio","slug":"failed-to-write-entire-datagram-to-socket","errorCode":null,"errorMessage":"failed to write entire datagram to socket","messagePattern":"failed to write entire datagram to socket","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"tokio-util/src/udp/frame.rs","lineNumber":165,"sourceCode":"        }\n\n        let Self {\n            ref socket,\n            ref mut out_addr,\n            ref mut wr,\n            ..\n        } = *self;\n\n        let n = ready!(socket.borrow().poll_send_to(cx, wr, *out_addr))?;\n\n        let wrote_all = n == self.wr.len();\n        self.wr.clear();\n        self.flushed = true;\n\n        let res = if wrote_all {\n            Ok(())\n        } else {\n            Err(io::Error::new(\n                io::ErrorKind::Other,\n                \"failed to write entire datagram to socket\",\n            )\n            .into())\n        };\n\n        Poll::Ready(res)\n    }\n\n    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {\n        ready!(self.poll_flush(cx))?;\n        Poll::Ready(Ok(()))\n    }\n}\n\nimpl<C, T> UdpFramed<C, T>\nwhere\n    T: Borrow<UdpSocket>,","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/tokio-rs/tokio/blob/625954f365727668cb02d04172b34f1149637728/tokio-util/src/udp/frame.rs#L147-L183","documentation":"Runtime error from `UdpFramed::poll_flush` (udp/frame.rs:165). After `poll_send_to`, the number of bytes written is less than the full datagram (`n != wr.len()`), so the entire datagram was not transmitted; Tokio returns `io::ErrorKind::Other`. UDP `send_to` is normally atomic, so a short send indicates the socket/OS refused part of the datagram.","triggerScenarios":"`UdpFramed` sink flush where `poll_send_to` returns fewer bytes than the encoded frame length: oversized datagram relative to MTU/socket limits, a connected socket quirk, or a non-standard `AsyncWrite`/socket shim.","commonSituations":"Datagram larger than the path MTU or the OS `SO_SNDBUF`; platform-specific short-send behavior; writing through a wrapper that fragments; mis-encoded frame larger than a single datagram should carry.","solutions":["Keep each encoded frame within the path MTU / OS datagram size limit (commonly <= 1472 bytes for IPv4 UDP, or <= 65467 for max IPv4).","Ensure one logical frame maps to one datagram when using `UdpFramed`.","Handle the error and decide to drop or retry that datagram.","Check and, if needed, raise `SO_SNDBUF` on the socket."],"exampleFix":"// before: encoding a frame larger than a datagram should carry\nlet framed = UdpFramed::new(socket, codec);\nframed.send((big_item, addr)).await?;\n\n// after: bound the encoded size per frame\nconst MAX_DGRAM: usize = 1472; // IPv4, typical MTU 1500 - 20 - 8\nif encoded_len(&item) > MAX_DGRAM {\n    return Err(anyhow!(\"datagram exceeds MTU\"));\n}\nframed.send((item, addr)).await?;","handlingStrategy":"try-catch","validationCode":"// Bound the encoded frame to a single datagram\nconst MAX_DGRAM: usize = 1472;\nif encoded_len(&item) > MAX_DGRAM {\n    return Err(io::Error::new(io::ErrorKind::Other, \"datagram exceeds MTU\").into());\n}","typeGuard":"fn is_short_datagram(e: &io::Error) -> bool {\n    e.kind() == io::ErrorKind::Other && e.to_string().contains(\"entire datagram\")\n}","tryCatchPattern":"if let Err(e) = framed.send((item, addr)).await {\n    if is_short_datagram(&e) { /* drop oversized datagram or shrink payload */ continue; }\n    return Err(e.into());\n}","preventionTips":["Map one logical frame to one UDP datagram.","Keep frames within the path MTU (commonly <= 1472 bytes for IPv4 UDP).","Raise `SO_SNDBUF` if the OS send buffer is the bottleneck."],"tags":["rust","tokio","tokio-util","udp","codec","network","runtime"],"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"}