Skip to content

MUX-H1 log lines still call getpeername(2) directly, bypassing SocketHandler::peer_addr and rendering peer=None after a RST #1524

Closed

Description

@FlorentinDUBOIS

MUX-H1 renders its peer= field with a live getpeername(2) that reaches past SocketHandler::peer_addr(), the trait method added precisely to stop that. This is the same defect #1341's Q5 fixed for MUX-H2 in #1360, left behind on the H1 side.

Found while auditing whether Q5 still held after #1501; it does, and this is what the audit turned up next to it.

The two sites

Both are in lib/src/protocol/mux/h1.rs, in the two log-context macros, and both are production:

  • h1.rs:56 — log_context!
  • h1.rs:85 — log_context_stream!

Both read:

peer = $self.socket.socket_ref().peer_addr().ok(),

$self.socket is Front: SocketHandler, so the trait method is in scope and available. The macro goes around it to the raw stream instead.

For contrast, the H2 macros at h2.rs:91 and h2.rs:125 read peer = $self.peer_address, a field on ConnectionH2 (h2.rs:720) seeded once per connection from socket.peer_addr() in H2Shell::new. ConnectionH1 has no equivalent field.

33 expansions of the two macros exist in h1.rs.

Why it matters, and what it costs

The reasons are #1341's, unchanged, and they are about observability rather than performance:

  • getpeername returns ENOTCONN after a peer RST, so the error! lines an operator reads during an incident render peer=None — on H1 today, where H2 now renders the address.
  • On a PROXY-protocol frontend the cached value is the advertised client; a live lookup is the load balancer. FrontRustls::peer_addr returns configured_peer, seeded from HttpsSession::peer_address at https.rs:562, which is exactly what makes the PROXY case correct — and the H1 macro does not consult it.
  • SessionTcpStream::peer_addr is cache-then-live for backend-connect failures; the H1 macro bypasses that too.

So for the same connection, an operator grepping one ULID can currently get a correct peer= on a MUX-H2 line and peer=None, or the load balancer's address, on a MUX-H1 line.

Do not argue this on performance. #1341 examined that for H2 and retracted it: there is no clock read in the macro, no expansion is unconditional per-frame or per-request, and _log_enabled! returns before the arguments are evaluated, so a filtered line pays nothing. The same reasoning applies here and the same conclusion should be drawn — the case is observability and consistency, nothing else.

Two possible fixes, and they are not equivalent

  1. Call the trait method. Replace both expressions with SocketHandler::peer_addr(&$self.socket). Two lines. Fixes the PROXY and cache-then-live cases immediately, because those live inside the implementors.
  2. Cache per connection, as H2 does — a peer_address field on ConnectionH1 seeded once at construction. Larger, and it is what makes the H2 field read structurally incapable of returning ENOTCONN rather than merely unlikely to.

(1) is the floor and closes most of the gap. (2) is what H2 ended up with, and only after 4eb51741, the first commit of #1471, hoisted the per-expansion call into a connection-owned field. Whether H1 should go straight there is a judgement about whether the two protocols' log paths ought to be the same shape — a reader comparing an H1 and an H2 line for the same session is the case this issue is about, so I lean toward (2), but (1) shipped alone would already be a real improvement and is trivially reviewable.

Asked for

Decide between (1) and (2), implement it, and test it. Each behaviour wants a test seen red before the fix lands — revert, show it failing, restore, show it passing. The H2 side already has that discipline applied and its tests carry their own "To SEE THIS RED" instructions naming the exact substitution; follow that pattern rather than inventing one.

The H2 equivalents to model on: socket.rs:1883, socket.rs:1705, h2.rs:11254, and h2.rs:11379 (which pins that the peer is read once per connection, not per expansion).

Not in scope

lib/src/protocol/pipe.rs:325 was checked and is already cache-first (session_address.or_else(...)). It is not a defect and should not be touched.

Every other socket_ref().peer_addr().ok() in the tree — https.rs:3559, https.rs:3563, h2.rs:11263, h2.rs:11295, socket.rs:1715, socket.rs:1805 — sits inside a #[cfg(test)] module and is deliberate red-proof scaffolding. Counted by reading each site's enclosing module, not by grep line count. Leave them alone.

Activity

  1. FlorentinDUBOIS commented on Sep 25, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    Correction to this issue's body, now applied.

    It attributed the H2 hoist — moving peer= out of a per-expansion call and into a connection-owned field — to #1501. That is wrong. #1501 is refactor(mux-h2): render the peer address where Debug named the socket: a Debug field swap plus an ensure_tls_flushed de-duplication, h2.rs +26/-… , and it never touches log_context!.

    The hoist is 4eb51741, the first of eight commits in #1471, refactor(mux-h2): read the log prefix peer from a connection-owned snapshot. Verified in-tree: that commit is where both macros go from

    peer = $self.socket.peer_addr(),
    

    to

    peer = $self.peer_address,
    

    and where the field and its seeding (peer_address: socket.peer_addr(),) are introduced.

    The body's code anchors were unaffected and remain correct — h2.rs:11379 is 4eb51741's own read-once-per-connection count test. Only the PR number in the "model on this" sentence was wrong, and that sentence is what a reader follows to find the precedent, so it mattered.

    A third live-lookup surface this issue does not name

    Found while implementing it, and outside its scope as written:

    • lib/src/protocol/mux/mod.rs:64 and :91 — the log_context! and log_context_lite! macros render peer = $self.frontend.socket().peer_addr().ok(),, which resolves to mio's inherent method rather than to SocketHandler::peer_addr. Same defect class, two more production sites. A grep for socket_ref().peer_addr() does not find them, which is why this issue's own survey missed them.
    • lib/src/protocol/mux/h1.rs:168 — ConnectionH1's Debug still renders .field("socket", &self.socket.socket_ref()), handing mio's Debug a local address, a peer address and a file descriptor. This is exactly what refactor(mux-h2): render the peer address where Debug named the socket #1501 changed on the H2 side and did not carry to H1.

    Neither is fixed here. Both want their own changeset, and the first in particular deserves a decision about whether every peer= slot in mux/ should be required to go through the trait — at which point the question is worth asking as a rule rather than site by site.

  2. FlorentinDUBOIS commented on Sep 25, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    Closed by #1526, merged as 6d6640bf.

    Option (2), the per-connection snapshot, was taken rather than (1), and what decided it is a case this issue's framing missed: option (1) fixes the PROXY and cache-then-live paths, but leaves ENOTCONN conditional on a cache the direct-accept routes may not hold — http.rs and https.rs both seed configured_peer from a best-effort peer_addr().ok() at accept, an Option precisely because that lookup can fail. A per-line trait call would still render peer=None after a reset on those routes.

    The cost argument inverted too. 4eb51741's expense on the H2 side was decoupling ConnectionH2 from Front — 113 of 140 touch points — and that decoupling does not exist here: ConnectionH1 already owns socket: Front, and the four other ConnectionH1 { … } sites are destructuring patterns with ... Production change: 2 macro lines, 1 field, 2 seeding lines, no signature changes.

    Three red-then-green recipes, each applied and the red observed. The one worth keeping is the third: it applies the option (1) shape and leaves only the read-once-per-connection count test red at left: 4, right: 1 while the other four stay green — reproducing 4eb51741's own documented negative result, and showing (2) is load-bearing rather than decorative.

    One gap stated rather than papered over: symptom 3's TLS variant is covered by composition with front_rustls_peer_snapshot_is_the_session_peer_not_the_accepted_socket, not by a direct test, because ConnectionH1<FrontRustls> needs https.rs's fixtures while the H1 macros are file-local.

    Still open, from the correction comment above: lib/src/protocol/mux/mod.rs:64 and :91 render peer = $self.frontend.socket().peer_addr().ok(), resolving to mio's inherent method rather than the trait — two production sites a socket_ref().peer_addr() grep cannot see. And h1.rs's Debug still hands mio's Debug a socket, which is what #1501 changed on the H2 side. Neither is fixed here; both want their own changeset.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions