hyperium/hyper · error · hyper::Error

error writing a body to connection

Error message

error writing a body to connection

What it means

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.

Source

Thrown at src/error.rs:414

    ))]
    pub(super) fn new_closed() -> Error {
        Error::new(Kind::ChannelClosed)
    }

    #[cfg(all(
        any(feature = "client", feature = "server"),
        any(feature = "http1", feature = "http2")
    ))]
    pub(super) fn new_body<E: Into<Cause>>(cause: E) -> Error {
        Error::new(Kind::Body).with(cause)
    }

    #[cfg(all(
        any(feature = "client", feature = "server"),
        any(feature = "http1", feature = "http2")
    ))]
    pub(super) fn new_body_write<E: Into<Cause>>(cause: E) -> Error {
        Error::new(Kind::BodyWrite).with(cause)
    }

    #[cfg(any(
        all(feature = "http1", any(feature = "client", feature = "server")),
        feature = "ffi"
    ))]
    pub(super) fn new_body_write_aborted() -> Error {
        Error::new(Kind::User(User::BodyWriteAborted))
    }

    fn new_user(user: User) -> Error {
        Error::new(Kind::User(user))
    }

    #[cfg(any(feature = "http1", feature = "http2"))]
    #[cfg(feature = "server")]
    pub(super) fn new_user_header() -> Error {
        Error::new_user(User::UnexpectedHeader)

View on GitHub (pinned to 084473f728)

Solutions

  1. Inspect Error::source() for the io::Error kind (BrokenPipe/ConnectionReset) to confirm the peer went away.
  2. For servers, this usually means the client gave up — log at debug/warn, not error — and stop writing.
  3. Bound your response generation so you don't keep producing a body nobody will receive (cancel the producer when the write fails).

Example fix

// before: keep producing a body even after the client disconnects
while let Some(chunk) = source.next().await {
    tx.send_data(chunk?).await?; // surfaces BodyWrite error late
}

// after: stop producing as soon as the sink reports the peer is gone
while let Some(chunk) = source.next().await {
    if tx.send_data(chunk?).await.is_err() {
        tracing::warn!("client went away, stopping body production");
        break;
    }
}
Defensive patterns

Strategy: try-catch

Type guard

// BodyWrite has no dedicated predicate; detect by checking it's not a parse/canceled/closed variant
fn looks_like_body_write(_e: &hyper::Error) -> bool { /* log + source inspect */ false }

Try / catch

while let Some(chunk) = producer.next().await {
    if tx.send_data(chunk?).await.is_err() {
        // peer went away; stop producing
        break;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hyperium/hyper@084473f728 (2026-08-06). Data as JSON: /data/errors/9656cfb488e0da36.json. Report an issue: GitHub.