Repository navigation
Description
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 toError, and the frontend emits RST_STREAM. This is correct.- everything else,
BodySize::Length(n)included: it pushesFlags { end_body: true, end_stream: true }and setsParsingPhase::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
- The backend answers
HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 1000000\r\n\r\nand then dies (crash, OOM kill, restart during a deploy) after 300000 body bytes, closing the socket cleanly. - H2 frontend: the client receives HEADERS (
content-length: 1000000), DATA totalling 300000 bytes, andEND_STREAM. H1 frontend: the client receives the sameContent-Lengthand 300000 bytes. sozu considers the response complete and, for a keep-alive client, keeps the connection open and waits for the next request. - sozu logs
200 … H1::Complete. No error metric or log line marks the truncation.
Consequences:
- An H2 client that does not enforce
content-lengthagainst 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-EncodingnorContent-Lengthis delimited by the close (rule 8). - RFC 9113 §8.1.1: a message whose DATA payload lengths do not sum to
content-lengthis malformed. An intermediary that knows the message is incomplete must not forward it as complete; the stream should end withRST_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) checksStream::ensure_content_length_zeroonEND_STREAMand 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-Lengthis treated as an aborted response: the client side is aborted (H2RST_STREAM, H1 connection closed without completing the message) and the log carries anSDtermination 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 inConnectionH1::terminate_close_delimited:warn!, the same (or a sibling) metric, kawa inError,RST_STREAM(INTERNAL_ERROR)to an H2 client, and the frontend connection closed without keep-alive for an H1 client. - Apply the same
body_sizediscrimination to theEndStreamAction::CloseDelimitedarm ofConnectionH2::end_stream, and toend_stream_decisionif it is simpler to decide there: onlyBodySize::Empty/close-delimited bodies may end withEND_STREAM. - Tests: an H1 backend with
Connection: close+Content-Length: 10that sends 4 body bytes then EOF must giveRST_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 noContent-Length, must still end cleanly.
Activity
fix(mux): end a Content-Length response truncated by a backend close …
fix(mux): end a Content-Length response truncated by a backend close …
fix(mux): end a Content-Length response truncated by a backend close …
fix(mux): end a Content-Length response truncated by a backend close …