{"id":"9656cfb488e0da36","repo":"hyperium/hyper","slug":"error-writing-a-body-to-connection","errorCode":null,"errorMessage":"error writing a body to connection","messagePattern":"error writing a body to connection","errorType":"exception","errorClass":"hyper::Error","httpStatus":null,"severity":"error","filePath":"src/error.rs","lineNumber":414,"sourceCode":"    ))]\n    pub(super) fn new_closed() -> Error {\n        Error::new(Kind::ChannelClosed)\n    }\n\n    #[cfg(all(\n        any(feature = \"client\", feature = \"server\"),\n        any(feature = \"http1\", feature = \"http2\")\n    ))]\n    pub(super) fn new_body<E: Into<Cause>>(cause: E) -> Error {\n        Error::new(Kind::Body).with(cause)\n    }\n\n    #[cfg(all(\n        any(feature = \"client\", feature = \"server\"),\n        any(feature = \"http1\", feature = \"http2\")\n    ))]\n    pub(super) fn new_body_write<E: Into<Cause>>(cause: E) -> Error {\n        Error::new(Kind::BodyWrite).with(cause)\n    }\n\n    #[cfg(any(\n        all(feature = \"http1\", any(feature = \"client\", feature = \"server\")),\n        feature = \"ffi\"\n    ))]\n    pub(super) fn new_body_write_aborted() -> Error {\n        Error::new(Kind::User(User::BodyWriteAborted))\n    }\n\n    fn new_user(user: User) -> Error {\n        Error::new(Kind::User(user))\n    }\n\n    #[cfg(any(feature = \"http1\", feature = \"http2\"))]\n    #[cfg(feature = \"server\")]\n    pub(super) fn new_user_header() -> Error {\n        Error::new_user(User::UnexpectedHeader)","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/hyperium/hyper/blob/084473f728f9d07b3be5845475aa2f62ed9ff579/src/error.rs#L396-L432","documentation":"Thrown via Error::new_body_write() (src/error.rs:414, Kind::BodyWrite). It means writing the outgoing body to the connection failed — the socket rejected a write (broken pipe, peer closed) while hyper was flushing body bytes. The underlying cause is attached. This is the write-side counterpart of Kind::Body.","triggerScenarios":"Writing body bytes to the stream returns an io::Error (broken pipe because the peer closed early); a client/server attempts to send more body after the peer has gone away. The dispatch/conn write path lifts the io::Error via new_body_write.","commonSituations":"The client closed the connection while the server was still streaming a large response (user navigated away / cancelled); backpressure from a slow consumer whose connection then died; sending a body to a peer that already responded and closed.","solutions":["Inspect Error::source() for the io::Error kind (BrokenPipe/ConnectionReset) to confirm the peer went away.","For servers, this usually means the client gave up — log at debug/warn, not error — and stop writing.","Bound your response generation so you don't keep producing a body nobody will receive (cancel the producer when the write fails)."],"exampleFix":"// before: keep producing a body even after the client disconnects\nwhile let Some(chunk) = source.next().await {\n    tx.send_data(chunk?).await?; // surfaces BodyWrite error late\n}\n\n// after: stop producing as soon as the sink reports the peer is gone\nwhile let Some(chunk) = source.next().await {\n    if tx.send_data(chunk?).await.is_err() {\n        tracing::warn!(\"client went away, stopping body production\");\n        break;\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"// BodyWrite has no dedicated predicate; detect by checking it's not a parse/canceled/closed variant\nfn looks_like_body_write(_e: &hyper::Error) -> bool { /* log + source inspect */ false }","tryCatchPattern":"while let Some(chunk) = producer.next().await {\n    if tx.send_data(chunk?).await.is_err() {\n        // peer went away; stop producing\n        break;\n    }\n}","preventionTips":["Stop generating body bytes as soon as send_data/send fails — don't keep producing for a dead peer.","For servers, treat a write failure to a gone client as a warning, not a hard error.","Bound producers with cancellation so a disconnected client stops upstream work."],"tags":["body","write","network","rust"],"analyzedSha":"084473f728f9d07b3be5845475aa2f62ed9ff579","analyzedAt":"2026-08-06T01:20:18.522Z","schemaVersion":2}