Repository navigation
Description
Summary
Two avoidable syscalls on the connection close path, measured on origin/main (f165a3d) with an LD_PRELOAD fd tracer and intentrace -p against one worker, 20 sequential requests (one connection each) through an HTTP listener (H1) and an HTTPS listener with --http2 (H2).
1. An EOF read together with data is not recorded as HUP
When socket_read returns (n > 0, SocketResult::Closed), update_readiness_after_read (lib/src/protocol/mux/mod.rs) clears READABLE but does not record that the peer's write half is closed. The backend close in Mux::ready_inner is gated on readiness.event.is_hup() || is_error(), so it waits for the kernel's EPOLLRDHUP edge, which is already queued: the next epoll_wait returns at once with that event and nothing else.
H1 trace excerpt (backend fd 17, frontend fd 16):
recv fd=17 ret=216
recv fd=17 ret=0 <- EOF observed here
writev fd=16 ret=253
epoll_wait ... ev=6:2005 <- extra wake-up, only to learn the same EOF
shutdown fd=17 how=1
close fd=17
This happened on 12 of 20 H1 requests. When EPOLLRDHUP arrives in the same event as the data, the existing path already closes the backend in the same pass and still forwards the data afterwards, so recording HUP on a read EOF reuses a path that is already exercised.
Expected gain: -1 epoll_wait per request when the EOF arrives with the last data.
2. shutdown(SHUT_WR) on a socket whose peer already closed
HttpSession::close, HttpsSession::close, Mux::close and the dead-backend sweep of Mux::ready_inner call shutdown(Shutdown::Write) unconditionally before the descriptor is closed. On the H2 frontend, after the client's close_notify has been read, it fails with ENOTCONN on every connection (20/20):
recv fd=16 ret=24 <- client close_notify
writev fd=16 ret=39 <- GOAWAY
writev fd=16 ret=24 <- our close_notify
shutdown fd=16 how=1 ret=-1 err=107 (ENOTCONN)
close fd=16
HAProxy skips the socket-layer shutdown once it has seen the read0 (conn_sock_shutw, include/haproxy/connection.h: "don't perform a clean shutdown if we're going to reset or if the shutr was already received"). On a descriptor that is not shared, shutdown(SHUT_WR) followed by close() sends nothing close() alone would not: FIN after queued data when the receive queue is empty, RST otherwise.
The TLS close_notify is not in scope: RFC 8446 §6.1 requires each party to send close_notify before closing its write side unless it already sent an error alert, whether or not the peer's close_notify was received (only the TLS 1.2 obligation to answer immediately was dropped). HAProxy keeps it too (conn_xprt_shutw runs unconditionally in mux_h2.c and mux_h1.c). On a reset peer FrontRustls::socket_write already short-circuits without a syscall.
Expected gain: -1 shutdown per connection on each side whose peer closed first (H2 frontend on every connection; H1 frontend and backend when they close first).
Proposed change
- Record
Ready::HUPinupdate_readiness_after_readwhen the read observedSocketResult::Closed, so the same pass closes the backend after its last bytes are parsed and forwarded. - Skip
shutdown(SHUT_WR)when the socket's readiness already carries HUP (read0 seen,EPOLLRDHUP, or reset), keep it otherwise so a peer that is still connected keeps getting an orderly FIN.
No change to half-close handling: a frontend HUP already closes the session today (Ready::from(&Event) maps is_read_closed() to HUP).
Activity
FlorentinDUBOIS commentedon Sep 26, 2026
Scope correction, implemented in #1605:
ConnectionH1::readable), not in the sharedupdate_readiness_after_read. A frontendClosedover TLS can be aclose_notifyon a TCP stream that is still open, which the kernel does not report as read-closed, and a frontend HUP closes the whole session. H2 backends still close on theEPOLLRDHUPedge (the change belongs inh2.rs).close_notify(FrontRustls::peer_disconnected), since the H2 close follows that read in the same pass with no epoll event in between.perf(mux): stop waiting for a read EOF and shutting down closed peers (…
test(mux): rewrite the #1603 EOF tests to the stop-on-short-read cont…
perf(socket): stop reading on a short read instead of reading until E…