Repository navigation
perf(mux-h2): send the final GOAWAY and the TLS close_notify in one write - #1608
Merged
Merged
Conversation
…rite Closing an H2 frontend over TLS on its final GOAWAY cost two writev(2) back to back in the same pass: the GOAWAY, flushed by H2Shell::flush_zero_to_socket through FrontRustls::socket_write, which runs write_tls at once, then the close_notify from H2Shell::close. When the zero buffer holds that final GOAWAY on a server connection with no stream, WINDOW_UPDATE or RST_STREAM left to write after it (ConnectionH2::zero_flush_closes_connection), the flush goes through the new SocketHandler::socket_write_then_close. FrontRustls hands rustls the GOAWAY, queues the alert behind it and flushes both records with one write_tls, which rustls sends with one write_vectored. The close_notify is still sent (RFC 8446 section 6.1) and still follows the GOAWAY; only the number of writes changes. The trait's default body writes and then calls socket_close, so other handlers behave as before. Release build, 20 curl --http2 requests each on its own TLS connection, LD_PRELOAD fd tracer: close writes 35-40 -> 20 per 20 connections, writev 6.85-7.15 -> 6.05-6.30 per connection, epoll_wait unchanged. The test the_final_goaway_and_close_notify_leave_in_one_tls_write counts the TLS writes from the GOAWAY to close over a real FrontRustls (1; 2 without the change) and reads the records off the wire in order. Closes #1607 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 27, 2026 01:49
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.
Closes #1607
What changes
Closing an H2 frontend over TLS on its final GOAWAY cost two
writev(2)back to back, in the same pass:FrontRustls::socket_writerunswrite_tlsright after handing rustls the plaintext, so the GOAWAY is on the wire before anyone can queue the alert behind it.SocketHandler::socket_write_then_close(buf). Default body:socket_write, thensocket_closeif the whole ofbufwas taken, which is what every other handler did already.FrontRustlsoverrides it:writer().write(buf), thensend_close_notify()only if all ofbufwas absorbed, then onewrite_tls, which rustls turns into onewrite_vectoredover both records (rustls-0.23.45/src/vecbuf.rs,ChunkVecBuffer::write_to).H2Shell::flush_zero_to_socketuses it whenConnectionH2::zero_flush_closes_connection()holds:Position::Server,H2State::GoAway(onlygoaway()sets it, after serialising the final GOAWAY),expect_write == Some(Zero)(the zero-buffer resume, not a GOAWAY deferred behind a half-written stream frame, nor a WINDOW_UPDATE/RST_STREAM batch), no stream, no pending WINDOW_UPDATE or RST_STREAM,close_notify_sentunset. The writable arm ofGoAwaythen closes;H2Shell::closefinds nothing left to drain.close_notify_sentis set once the whole buffer was taken, so neitherinitiate_close_notifynordrain_tls_close_notifyqueues a second alert.The
close_notifystays mandatory (RFC 8446 §6.1) and still follows the GOAWAY on the wire; only the number of writes changes. rustls encrypts application data straight into its send buffer once the handshake is over, which it always is on a connection the mux drives, so the alert queued after the plaintext is the next record.HAProxy (
0ceb8c65f) keeps two writes on this path:h2c_send_goaway_error(src/mux_h2.c:2504) puts the GOAWAY in the mux buffer, the timeout path flushes it withxprt->snd_buf(src/mux_h2.c:5540-5548), andh2_release(src/mux_h2.c:1607) then callsconn_xprt_shutw(src/mux_h2.c:1658), whosessl_sock_shutw(src/ssl_sock.c:8001) issues its ownSSL_shutdown(src/ssl_sock.c:8041). There is noh2_shutwin that tree:h2_do_shutw(src/mux_h2.c:5963) andh2_shut(src/mux_h2.c:6090) handle a stream, not the connection.Measurements
Release build, one worker, python
http.serverbackend, 20 sequentialcurl --http2requests each on its own TLS connection, LOAD1 < 8,b19d94f8before, this branch after. fdtrace =LD_PRELOADfd tracer; intentrace =intentrace -p, ANSI stripped, anchored on the syscall name.Initial measurement on
b19d94f8: fdtracewritev6.90/conn,epoll_wait8.00/conn, total 33.80/conn; intentracewritev6.00,epoll_wait5.00, total 42.10.len=39,len=24,len=63)len=63writeswritev/ connepoll_wait/ connwritev/ connThe connections without a merged write closed on the client's HUP (
ev=5:2005) before any GOAWAY was read: sozu sends the 24-byte alert alone there, before and after. Underintentrace -pevery connection takes that path (wrote 39 Bytes: 0 on all three runs,wrote 24 Bytes: 20), because ptrace slows the worker enough for curl's GOAWAY,close_notifyand FIN to all be queued when it wakes; so intentrace cannot see this change.writevalso varies with how the response split (181 + 70 against 229, on 0 to 6 connections per run).H2 backends: nothing done for the EOF-as-HUP of #1605
#1605 records a backend EOF read with the last bytes as HUP for H1 backends. For H2 backends there is nothing measurable to gain, with or without #1606:
handle_goaway_framecallsgoaway(), which clearsexpect_read, andConnectionH2::force_disconnect'sPosition::Clientarm setsreadiness.event = Ready::HUPitself, so the backend is closed in the same pass.H2ReadTarget::Fill { amount }: 9 header bytes, then the payload). perf(socket): stop reading on a short read instead of reading until EAGAIN #1606'splain_socket_readanswersContinuewhensz == buf.len(), so a complete frame is not a short read and the next header read meets(0, Closed)in the same pass, exactly as onmain. perf(socket): stop reading on a short read instead of reading until EAGAIN #1606 moves nothing here.epoll_waitand therecv: at most oneepoll_waitper backend H2 connection, never per request, and not measurable with an H1 backend.Tests (seen red first)
the_final_goaway_and_close_notify_leave_in_one_tls_write(h2.rs): over a realFrontRustlson a loopback socket, drivesgoaway(NoError), thewritablepass (which must answerCloseSession) andclose, and counts everywrite_tlsholding a record (crate::socket::tls_writes, a#[cfg(test)]thread-local counter in the newflush_tlshelper everyFrontRustlswrite goes through). Then it reads the wire raw and checks the records in order (39 bytes, then 24), and decrypts them: the 17-byte GOAWAY withNO_ERROR, then a clean end of stream, which rustls reports only afterclose_notify.zero_flush_closes_connectionbranch dropped:left: 2, right: 1on the write count.send_close_notifyqueued beforewriter().write: records[24, 39].Checks
cargo build --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd,cargo +nightly fmt --all -- --check, clippy with the same features--all-targets -- -D warnings, tests forsozu-lib(1217 passed),sozu,sozu-command-lib,sozu-e2e -- --skip tests::fuzz_tests::(449 passed),sozu-simtest and clippy under--cfg tokio_unstable,RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked, andcheck_doc_citations.py(--self-test,--root .,--base b19d94f8…,--audit --root .): all exit 0. 14 line citations indoc/andLIFECYCLE.mdre-anchored to the same text in the same function.Rebase note for #1606: this branch touches
socket.rson the write side only (flush_tlsat the fivewrite_tlssites, the trait method, theFrontRustlsoverride); its hunks start at old lines 65, 582, 931, 972, 1124, 1157, 1192 and 1249, none overlapping #1606's (192, 608–733 on the read path, 1263).mux/mod.rsis untouched.Signed-off-by: Florentin Dubois [email protected]