Skip to content

fix(proxy-protocol): drain a PROXY-v2 header coalesced with the client half-close - #1836

Merged
FlorentinDUBOIS merged 6 commits into
mainfrom
fix/tcp-proxy-protocol-half-close
Oct 3, 2026
Merged

FlorentinDUBOIS merged 6 commits into
mainfrom
fix/tcp-proxy-protocol-half-close

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #1823. This does not close it: #1823 also expects the client to receive the response after its half-close, and on TCP that is blocked by the pipe's close-after-flush policy, which affects every TCP session. That policy question is now #1840.

Summary

On Linux, a client that writes its PROXY-v2 header and payload and then calls shutdown(SHUT_WR) can show up as one READABLE | HUP epoll event (EPOLLIN | EPOLLRDHUP, without WRITE_CLOSED). The expect-proxy stage acted on HUP before it read anything, so it threw away a header that was complete and already queued.

CAUSE: TcpSession::ready_inner (lib/src/tcp.rs) runs its frontend-HUP check before the readable loop -> TcpSession::front_hup's wildcard arm returns Close for ExpectProxyProtocol -> SYMPTOM: ExpectProxyProtocol::readable (lib/src/protocol/proxy_protocol/expect.rs) never parses the queued header, and the session closes before it dials the backend. The HTTP/HTTPS expect stage, SessionState::ready for ExpectProxyProtocol, had the same if hup { return Close } ahead of its loop.

Fix

  • TCP (TcpSession::ready_inner): while the session is in ExpectProxyProtocol and READABLE and HUP are both set, it now calls readable before the HUP check. The loop is bounded by MAX_LOOP_ITERATIONS.
    • A parsed header returns Upgrade. The recursive ready() dials the backend and hands the HUP, still set, to Pipe::frontend_hup, which keeps the session while request bytes the expect stage did not read remain to forward. Exactly what gets forwarded depends on how many bytes readable consumed as header; the separate, pre-existing short-header issue is being fixed in another PR.
    • An empty or truncated header ends with READABLE cleared, because the zero-byte read removes it. The session then closes through readable's zero-byte arm or the unchanged front_hup path. No backend is dialed.
  • HTTP/HTTPS (SessionState::ready for ExpectProxyProtocol): HUP now closes only once READABLE is gone. The check moved inside the loop, so a lone HUP still closes and never spins.
  • front_hup and Pipe are unchanged.

Behaviour notes / scope

  • Response after half-close is not covered (existing behaviour, unchanged). Pipe::check_connections (lib/src/protocol/pipe.rs) keeps a pipe whose frontend is Closed or WriteOpen open only while request bytes are in flight. Once the payload is flushed, a TCP pipe closes before the backend answers. This applies to every TCP session, with or without a PROXY header, and try_tcp_proxy_half_close already says it makes no promise about this. Plain TCP with no PROXY protocol delivered the response 0 of 40 times. The ported test asserts that the backend receives the payload and leaves this policy unasserted. Tracked as a policy question in TCP pipe: keep the response half open after a client half-close? #1840.
  • Health-check logging change: on Linux a zero-byte health check (connect then FIN) always arrives as READABLE | HUP. Before this PR it always went through TcpSession::front_hup → TcpSession::log_request, which wrote one TCP access-log line and recorded the end-of-session request/service timers. It now closes through the expect stage's zero-byte branch, so zero-byte health checks no longer emit a TCP access-log line or end-of-session timers. A truncated header followed by FIN still logs one.
  • Relay (RelayProxyProtocol): reported, not fixed. Its readable returns Continue after parsing and needs back_writable to relay the header, so draining first would not help; a coalesced HUP still closes it. SendProxyProtocol has no READABLE interest, and SniPreread keeps its own front_hup arm.
  • HTTP/HTTPS downstream: after the upgrade, HUP reaches the Mux, which the mux_tests proxy-protocol HUP e2e tests cover. On HTTPS it reaches TlsHandshake::ready, which already closes on HUP only when no read is due (fix(mux): a half-closed TLS H1 client loses the end of a large response #1779). I did not add a separate HTTPS e2e test for this path.

Tests

New lib unit tests. Each uses a real loopback socket and a real epoll observation of the coalesced READABLE | HUP:

  • tcp::sni_routing_tests::expect_proxy_half_close_drains_payload_to_the_backend: the session stays open after the first ready() and leaves the expect state, the backend gets dialed, and the backend receives exactly the payload queued before FIN. RED on origin/main at the !closed assertion.
  • tcp::sni_routing_tests::expect_proxy_zero_byte_half_close_closes_without_dialing: a zero-byte health check closes, no backend is dialed, and no backend token is set.
  • tcp::sni_routing_tests::expect_proxy_partial_header_half_close_closes_without_dialing: a truncated header plus FIN closes and dials nothing.
  • protocol::proxy_protocol::expect::expect_test::ready_parses_a_header_coalesced_with_the_client_half_close: the HTTP/HTTPS stage returns Upgrade, parses the source, and keeps HUP. RED on origin/main.
  • protocol::proxy_protocol::expect::expect_test::ready_closes_a_half_close_without_a_complete_header: zero bytes give Close; a truncated header gives Close with all 10 bytes consumed. RED on origin/main at the index assertion.

RED was confirmed by removing the two production hunks: the 3 tests marked RED failed. GREEN after restoring them.

Validation

  • cargo +nightly fmt --all -- --check -> exit 0
  • cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings -> exit 0
  • RUSTDOCFLAGS="-D warnings" cargo +1.93.1 doc --no-deps --locked -p sozu-lib -> exit 0
  • cargo +1.93.1 test -p sozu-lib --lib --all-features --locked -> 1596 passed, 1 failed: protocol::pipe::tests::splice_readable_eof_drains_kernel_pipe_bytes_before_closing. Its own message is "environment precondition not met: splice pipe capacity is 8192 bytes ... this is a host limit", from a shared host with fs.pipe-user-pages-soft exhausted. It passed 3 of 3 isolated runs. pipe.rs is not touched.
  • Targeted e2e (--features opentelemetry,splice,simd, filters tcp_tests:: proxy_protocol half_close expect_proxy) -> 38 of 39 passed, including mux_tests::test_proxy_protocol_{hup,keepalive_hup,partial_header,request,local_healthcheck,af_unspec_healthcheck,mixed_traffic}, test_rapid_proxy_protocol_healthchecks, tcp_tests::test_tcp_expect_proxy_zero_bytes_keeps_the_worker_alive, tcp_tests::test_tcp_proxy_half_close and tcp_tests::test_tcp_proxy_protocol_v2. The one failure was tls_tests::test_plain_client_half_close_after_request_receives_large_response: a plain HTTP listener without expect_proxy, so it never enters ExpectProxyProtocol. The 8 MiB body was truncated, and the test is also flaky in isolation (2 of 3 isolated runs passed).
  • tcp_sni_tests:: and via_proxy_v2 e2e -> 20 of 20 passed
  • python3 .github/scripts/check_doc_citations.py --root . --base origin/main -> all OK

Docs: lib/src/protocol/proxy_protocol/LIFECYCLE.md §2.1 now describes the HUP-after-drain ordering. CHANGELOG has an entry under Unreleased → Fixed.

…t half-close

On Linux a client that writes its PROXY-v2 header and payload and then
calls shutdown(SHUT_WR) can be reported as one READABLE|HUP epoll event.
TcpSession::ready_inner handled the frontend HUP before the readable
bytes, and TcpSession::front_hup closes the ExpectProxyProtocol state, so
the complete header was never parsed and the session closed before the
backend dial. SessionState::ready for ExpectProxyProtocol, the HTTP and
HTTPS listeners' expect stage, closed on HUP the same way.

Drain the expect state's readable bytes before honouring HUP. A parsed
header upgrades and hands HUP on to the next stage (on TCP,
Pipe::frontend_hup forwards the payload queued behind the header); a
zero-byte bare-TCP healthcheck or a truncated header ends with READABLE
cleared and still closes without dialing a backend.

Fixes #1823

Signed-off-by: Florentin Dubois <[email protected]>
…n drain claims

A zero-byte health check (connect then FIN) is always reported as
READABLE|HUP on Linux, so before this change it always went through
TcpSession::front_hup and logged a TCP access line plus end-of-session
timers; it now closes through the expect stage's zero-byte branch and
logs neither. Say so plainly, note that the response after a client
half-close is still not delivered (#1840), avoid claiming the pipe
forwards exactly the payload queued behind the header, and correct the
SniPreread wording in TcpSession::ready_inner.

Refs #1823

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 4b57617 into main Oct 3, 2026
23 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Oct 4, 2026
## Problem

`ExpectProxyProtocol` read inbound PROXY v2 data in fixed 28-, 52- and 232-byte stages. When a valid short header and application payload arrived in one client write, the socket read could pull payload into the header staging buffer. The parser returned that payload as its remainder, but the TCP, HTTP and HTTPS upgrades discarded the staging buffer.

A 16-byte `LOCAL` header therefore removed the first 12 payload bytes; IPv4 and IPv6 headers with TLVs could lose more.

A valid PROXY v2 header that filled the complete 232-byte expect buffer triggered a second defect: the expect stage removed READABLE interest. HTTPS copied that interest into the TLS handshake, so neither a queued nor a later ClientHello could be read and the connection stalled.

## Contract and fix

Read the fixed 16-byte PROXY v2 prelude first, derive the exact target as `16 + len`, reject a declared total above Sōzu's existing 232-byte capacity before another read, then read only to that target. Application bytes coalesced after the header remain on the socket for the downstream protocol.

The accepted 232-byte boundary, malformed-header rejection and public `HeaderLen` API remain intact. A complete maximum-size header no longer removes READABLE interest before the protocol transition, so HTTPS can consume the ClientHello. The documentation distinguishes invalid signatures, which fail parsing, from a valid prelude whose declared total exceeds the 232-byte capacity. Runtime ingress accepts PROXY v2; v1 remains only a legacy header model.

## Scope

Nine files form one behavior, test and documentation changeset:

- `lib/src/protocol/proxy_protocol/expect.rs`: exact-length reads, capacity rejection and preserved READABLE interest at the accepted 232-byte boundary;
- `e2e/src/tests/tcp_tests.rs`: real worker/backend regression for header totals 16, 28, 36, 60 and 232;
- `e2e/src/tests/proxy_protocol_payload_tests.rs` and `e2e/src/tests/mod.rs`: repeated TCP, HTTP and HTTPS regressions for coalesced and fragmented delivery, all accepted v2 header shapes, the exact 232-byte boundary, and refused v1, oversized and malformed inputs;
- `lib/src/protocol/proxy_protocol/LIFECYCLE.md`: exact-read, handoff, 232-byte interest, malformed input and merged half-close contracts;
- `doc/configure.md`: v2-only configuration and metric descriptions;
- `lib/README.md` and `lib/src/protocol/proxy_protocol/mod.rs`: v2 runtime versus legacy v1 model;
- `CHANGELOG.md`: both client-visible failures and their corrected behavior.

## Verification

On `main` after #1836 and before the exact-read correction, the targeted TCP E2E remained red, exit 101 (`0 passed; 1 failed; 623 filtered`). Backend observations were `17/29` for LOCAL-16, `29/29` for the IPv4-28 positive witness, `13/29` for IPv4+TLV-36, `0/29` for IPv6+TLV-60 and `29/29` for LOCAL-232. The worker stopped cleanly.

On `13e07772497f96830410dc0b7afb0599c96f4b5b`, before the 232-byte HTTPS correction, the unchanged HTTPS regression was red (`0 passed; 1 failed; 627 filtered`): all eight shorter coalesced and fragmented witnesses returned HTTP 200, while both exact-232 variants received no answer. The v1, 233-byte and malformed inputs remained refused without a backend dial, and the worker stopped cleanly.

On current head `02b7eceb73637c90f511b73d4969470b769b4af4`:

- the exact HTTPS regression passed (`1 passed; 0 failed; 627 filtered`) with both exact-232 variants returning HTTP 200;
- the complete TCP/HTTP/HTTPS payload module passed (`3 passed; 0 failed; 625 filtered`), with each test requiring two consecutive clean executions through `repeat_until_error_or`;
- the merged #1836 `READABLE | HUP` oracle passed (`1 passed; 0 failed; 1572 filtered`);
- scoped Clippy for `sozu-lib` and `sozu-e2e`, all targets, features `opentelemetry,splice,simd`, with warnings denied, passed;
- CI nightly rustfmt, documentation citation drift validation against `84f12bb1c139711a87089602cf4b6eaf3deeaad3`, and `git diff --check` passed;
- both commits retain signatures and sign-offs.

The original exact-read patch SHA-256 is `8e22297330000bdb3696838f177da87b382ad4700f2af3c075b807b288a885e5`. The 232-byte HTTPS correction from `13e07772...` to `02b7eceb...` has patch SHA-256 `6340a17e9e42d144ea67e1421d616dbe81ed414807fc93474d56ab931954900e`. Independent source review accepted the current correction; its report SHA-256 is `416ee19740c910e2542e7514f1994f94d631f954323d26ad75094d73170ea152`.

## Boundaries

The runtime regressions cover TCP, HTTP and HTTPS, coalesced and fragmented writes, accepted totals 16, 28, 36, 52, 60 and 232 where each protocol permits them, plus rejected v1, 233-byte and undersized address inputs. The focused #1836 unit oracle preserves parsing before a coalesced half-close; the broader TCP half-close policy tracked by #1840 remains outside this change.

Fixes #1841



Signed-off-by: Florentin Dubois <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant