Repository navigation
fix(proxy-protocol): forward the header a RelayHeader cluster never sent - #1348
Merged
Merged
Conversation
`RelayProxyProtocol::readable` consumed every byte it had just read — the PROXY-v2 header included — before `back_writable` had forwarded any of it. One line, two defects. A `RelayHeader` cluster exists so the backend receives the client's own header verbatim, and it was receiving nothing: this state re-serializes nothing, `back_writable` forwards the buffered prefix rather than rebuilding a header from `addresses`, so consuming that prefix destroyed the only copy. The comment at the parse site asserted the opposite and is corrected. `back_writable` could not then terminate. With the buffer emptied it looped on `socket.write(&[])`, which a connected non-blocking socket answers `Ok(0)`: `cursor_header` never advanced toward `header_size`, so that exit was unreachable and the loop's only other exit was its `Err` arm. The loop sits inside one `back_writable` call, and `MAX_LOOP_ITERATIONS` bounds the outer dispatch loop in `ready_inner`, not this inner one. `51aedab9` (2018-10-18) introduced it and it has shipped since 0.10.0. The `BufferQueue` it replaced kept the parse cursor and the output queue separate, so `readable` ran `consume_parsed_data` and `slice_output`: the header was retired as parsed input while those same bytes stayed queued for output, and `back_writable` drained them with `next_output_data` / `consume_output_data`. Flattening to a single cursor collapsed both into one `consume(sz)` and dropped the output half. This restores that behaviour on the single-cursor buffer: `readable` consumes nothing, `back_writable` consumes exactly as it forwards. Bytes the client pipelined into the same read stay in place for the pipe phase — `into_pipe` hands the same `Checkout` to `Pipe::new`, whose `arm_inherited_buffer_writes` arms the backend WRITABLE readiness when it is non-empty. A zero-length write now drops that event and yields, and the write is clamped to the unsent header tail, so a future read path that consumed the header again would cost one wasted syscall rather than a non-terminating loop. Three adjacent defects the forwarding fix makes reachable are fixed with it. `back_writable`'s `Err` arm reset both readiness words for every error kind, which was dead while the buffer was always empty; `WouldBlock` now clears only the stale WRITABLE event and keeps the interest, as `send.rs` does, and `Interrupted` leaves both untouched so `ready_inner` retries under its own bound. `into_pipe` restored readiness with a bare event assignment that overwrote the arm `Pipe::new` had just set — the pattern `build_pipe_from_preread` forbids, and the last pipe-entry path still using it — and now calls `restore_readiness_events`. `readable` had no zero-length-read guard, so a client that declared a large length and stalled left READABLE set and had `ready_inner` re-enter until `MAX_LOOP_ITERATIONS`. The SNI-preread `RelayHeader` path was never affected: `upgrade_sni_preread` routes it to `build_pipe_from_preread` without consuming the header and never builds a `RelayProxyProtocol`, which is why the existing e2e test covered that path and not this one. `RelayProxyProtocol`'s own forwarding had no coverage at all; it now has eight tests, each seen red under a documented mutation. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 18, 2026 22:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
RelayHeadercluster exists so the backend receives the client's own PROXY-v2 header verbatim. Itwas receiving nothing at all, and the write loop that should have sent it could not terminate.
The defect
RelayProxyProtocol::readableconsumed every byte it had just read — the header included — beforeback_writablehad forwarded any of it. One line, two consequences.The header was never forwarded. This state re-serializes nothing:
back_writableforwards thebuffered prefix rather than rebuilding a header from
addresses, so consuming that prefix destroyedthe only copy. The comment at the parse site asserted the opposite — "The header bytes themselves are
still forwarded verbatim onto the backend by
back_writable" — and is corrected.back_writablecould not terminate. With the buffer emptied it looped onsocket.write(&[]),which a connected non-blocking socket answers
Ok(0):cursor_headernever advanced towardheader_size, so that exit was unreachable and the loop's only other exit was itsErrarm. The loopsits inside a single
back_writablecall —MAX_LOOP_ITERATIONS(lib/src/tcp.rs:1403) bounds theouter dispatch loop in
ready_inner, not this inner one.Reached by a cluster configured with both
expect_proxyandsend_proxy, which yieldsProxyProtocolConfig::RelayHeaderand buildsTcpStateMachine::RelayProxyProtocol.Provenance
Not a recent regression, and not merely pre-existing:
51aedab9(2018-10-18) introduced it, andit has shipped in every release since 0.10.0.
The
BufferQueuethat commit replaced kept the parse cursor and the output queue as two separatethings, so
readableranconsume_parsed_data(sz)andslice_output(sz)— the header wasretired as parsed input while those same bytes stayed queued for output, and
back_writabledrainedthem with
next_output_data()/consume_output_data(sz). Flattening the buffer to a single cursorcollapsed both calls into one
consume(sz)and silently dropped the output half.This restores that behaviour on the single-cursor buffer:
readableconsumes nothing,back_writableconsumes exactly as it forwards.
Pipelined payload
Bytes the client pipelined into the same read stay in place for the pipe phase.
into_pipehands thesame
CheckouttoPipe::new, whosearm_inherited_buffer_writesarms the backend WRITABLEreadiness exactly when it is non-empty, so the tail is flushed rather than dropped.
Three adjacent defects, fixed because this makes them reachable
back_writable'sErrarm reset both readiness words for every error kind. Dead while thebuffer was always empty — the loop only ever called
write(&[])and gotOk(0)— but a relay thatreally writes can be backpressured, and a v2 header is
16 + lenwithlen: u16.WouldBlocknowclears only the stale WRITABLE event and keeps the interest, as
send.rsdoes, so the next epolledge resumes a half-written header instead of the session dying at timeout with a truncated header
on the backend.
Interruptedleaves both words untouched soready_innerretries under its ownbound — removing WRITABLE there would be a lost wakeup, since the socket did not change state.
into_piperestored readiness with a bare event assignment, overwriting the armPipe::newhad just set — the exact pattern
build_pipe_from_preread's comment forbids, and the last of thethree pipe-entry paths still using it. Now calls
restore_readiness_events.readablehad no zero-length-read guard.tcp_socket_readreturns(0, Continue)as soon asthe slice it is handed is empty, which is what
space()yields on a full buffer, so a client thatdeclared a large
lenand stalled hadready_innerre-enter untilMAX_LOOP_ITERATIONS. Nowdrops the READABLE event, as
expect.rsdoes.Why it survived eight years
The only existing test, e2e's
try_tcp_sni_relay_proxy_forwards_header_verbatim, uses an SNI listener,so
upgrade_sni_prereadroutes it straight tobuild_pipe_from_prereadwithout consuming theheader and never builds a
RelayProxyProtocol. That path was always correct.RelayProxyProtocol's own forwarding had no coverage whatsoever.Tests — eight, each seen red under a documented mutation
back_writable_returns_instead_of_spinning,back_writable_forwards_the_header_verbatim,back_writable_leaves_pipelined_payload_for_the_pipe_phase,a_zero_length_write_yields_instead_of_spinning,back_writable_parks_the_session_on_a_backpressured_backend,readable_yields_on_a_zero_length_read,back_writable_keeps_its_readiness_when_a_signal_interrupts_the_write— plusinto_pipe_rearms_the_backend_write_for_pipelined_payloadinlib/src/tcp.rs, because the upgradeneeds a
TcpListenerwhose constructor and fields are private to that module.The EINTR test drives a real
socketpairfilled to zero free space withMSG_DONTWAIT, then signalsthe thread with
SIGUSR1installed withoutSA_RESTART, so the parked send returnsInterruptedrather than a partial write. That arm is defensive — production backend sockets come from
mio::net::TcpStream::connectand are always non-blocking, so theirsendanswersEAGAIN— whichis why exercising it needs a socketpair at all.
The test harness itself was corrected twice during review: it gated on a quantity the code under test
mutates, so a consumption defect stalled the harness and reddened every test in the module without
reaching the code under test. It now gates on
metrics.bin(bytes read off the socket), and thepre-
back_writablestages report failure as a value with their own message rather than surfacing asthe wedge assertion.