Skip to content

mux: a Content-Length response truncated by a backend close is forwarded as complete #1633

Closed

Description

@FlorentinDUBOIS

Summary

When an H1 backend answers Connection: close with a Content-Length body and closes the connection before the whole body has arrived, sozu ends the response as if it were complete. The truncated body leaves with a clean end of message (END_STREAM to an H2 client, a completed response to an H1 client), the original Content-Length header is forwarded unchanged, and the access log records H1::Complete with the backend's status. Only a chunked body is treated as truncated.

Observed by reading origin/main (58550dd4).

Mechanism

ConnectionH1::readable (lib/src/protocol/mux/h1.rs) handles a backend EOF (SocketResult::Closed) in two places: the zero-byte read branch, and the "data and EOF in one read" branch after kawa::h1::parse. Both call ConnectionH1::terminate_close_delimited when the position is client, the response is in a main phase, not terminated, and keep_alive_backend is false. keep_alive_backend goes false when the response carries Connection: close (HttpContext::on_response_headers, lib/src/protocol/kawa_h1/editor.rs).

ConnectionH1::terminate_close_delimited distinguishes only two cases:

  • BodySize::Chunked: warn!, h1.backend_eof_before_message_complete, the kawa goes to Error, and the frontend emits RST_STREAM. This is correct.
  • everything else, BodySize::Length(n) included: it pushes Flags { end_body: true, end_stream: true } and sets ParsingPhase::Terminated.

The second branch is only right for a close-delimited body, meaning no Content-Length and no chunked coding (RFC 9112 §6.3 rule 8). For BodySize::Length(n), kawa.expects > 0 at EOF means the message is incomplete, and it is terminated as if it were complete.

The same shape exists on the H2 frontend side. When the backend connection goes away without an EOF being seen by that read path (a socket error or a backend timeout), end_stream_decision (lib/src/protocol/mux/shared.rs) answers EndStreamAction::CloseDelimited for a main-phase response with keep_alive_backend == false. ConnectionH2::end_stream (lib/src/protocol/mux/h2.rs), Position::Server arm, then pushes the same end_stream flags whatever the body_size. (#1630 adds a chunked guard to that arm, not a Length one.)

Scenario

  1. The backend answers HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 1000000\r\n\r\n and then dies (crash, OOM kill, restart during a deploy) after 300000 body bytes, closing the socket cleanly.
  2. H2 frontend: the client receives HEADERS (content-length: 1000000), DATA totalling 300000 bytes, and END_STREAM. H1 frontend: the client receives the same Content-Length and 300000 bytes. sozu considers the response complete and, for a keep-alive client, keeps the connection open and waits for the next request.
  3. sozu logs 200 … H1::Complete. No error metric or log line marks the truncation.

Consequences:

  • An H2 client that does not enforce content-length against the received DATA stores or serves a truncated object as complete. This covers caches, CDNs chaining to sozu, and SDKs.
  • An H1 client waits for the missing bytes until a timeout, then sees a short read on close. It cannot tell a proxy truncation from a backend one.
  • H1 pipelining: requests the client pipelined behind this one are answered on the same connection. The next response's bytes arrive where the client still expects the body of the truncated one, so the responses desynchronise on that connection. Plausible by reading, not reproduced.
  • Monitoring: the request counts as a success.

References

  • RFC 9112 §6.3 (rule 5 and the closing paragraph): if the sender closes the connection before the indicated number of octets is received, the recipient MUST consider the message incomplete and close the connection. Only a response with neither Transfer-Encoding nor Content-Length is delimited by the close (rule 8).
  • RFC 9113 §8.1.1: a message whose DATA payload lengths do not sum to content-length is malformed. An intermediary that knows the message is incomplete must not forward it as complete; the stream should end with RST_STREAM, as sozu already does for a truncated chunked body.
  • hyperium/h2: the receive path enforces content-length. Recv::recv_data (src/proto/streams/recv.rs) checks Stream::ensure_content_length_zero on END_STREAM and raises a stream-level PROTOCOL_ERROR ("content-length underflow"). A hyper client therefore surfaces an error, but only because it re-checks what sozu forwarded; clients that do not check see a complete response.
  • HAProxy: to my knowledge, a server close before the announced Content-Length is treated as an aborted response: the client side is aborted (H2 RST_STREAM, H1 connection closed without completing the message) and the log carries an SD termination state. Not re-checked against the current source.

Expected

  • On an EOF with BodySize::Length(n) and bytes still expected, treat the response like the chunked case in ConnectionH1::terminate_close_delimited: warn!, the same (or a sibling) metric, kawa in Error, RST_STREAM(INTERNAL_ERROR) to an H2 client, and the frontend connection closed without keep-alive for an H1 client.
  • Apply the same body_size discrimination to the EndStreamAction::CloseDelimited arm of ConnectionH2::end_stream, and to end_stream_decision if it is simpler to decide there: only BodySize::Empty/close-delimited bodies may end with END_STREAM.
  • Tests: an H1 backend with Connection: close + Content-Length: 10 that sends 4 body bytes then EOF must give RST_STREAM (H2 frontend) or a closed connection with no keep-alive reset (H1 frontend). A 10-byte body followed by EOF, and a close-delimited body with no Content-Length, must still end cleanly.

Activity

  1. added 3 commits that reference this issue on Sep 28, 2026
    046092d
    32b7990
    688cc25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions