Repository navigation
Conversation
A downstream HTTP/2 connection starts its graceful shutdown (GOAWAY, then drain) only when the server receives a shutdown signal. Code handling a request cannot ask for the same on its own connection: the session has no handle to the connection, and `Session::set_keepalive(None)`, which closes an HTTP/1.x connection after the response, is a no-op for HTTP/2. Give each connection in `accept_downstream_sessions` a `Notify` and hand it to every session the loop accepts. The new `v2::server::HttpSession::graceful_shutdown_connection()` notifies it, and the accept loop then runs the drain it already runs on a shutdown signal, for that connection only: `graceful_shutdown()` on the h2 connection, keep surfacing the streams the codec still yields, and return once the connection is done. Other connections are not affected. `Session::graceful_shutdown_connection()` dispatches it. On HTTP/1.x it disables keepalive like `set_keepalive(None)`; subrequest and custom sessions, and h2 sessions not accepted by the accept loop, return false. Without a call nothing changes: the new select! branch is never woken. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Resolves #1125
What
Adds a way for code handling a request to start the graceful shutdown of its own downstream connection, without a server shutdown:
v2::server::HttpSession::graceful_shutdown_connection(&self) -> boolserver::Session::graceful_shutdown_connection(&mut self) -> boolSemantics:
accept_downstream_sessionsalready runs on a shutdown signal, limited to one connection. Other connections are not affected.set_keepalive(None); the connection is closed after the current response.falseand does nothing for subrequest and custom sessions, and for HTTP/2 sessions not accepted byaccept_downstream_sessions(for example, created withHttpSession::from_h2_conn). Returnstrueif the shutdown was requested, including when another stream of the same connection already requested it.How
accept_downstream_sessionscreates onetokio::sync::Notifyper connection and stores a clone of theArcin eachHttpSessionit accepts (a new private field,Nonefor sessions created elsewhere). Theselect!in the accept loop gets one more branch, right after the shutdown-signal branch, that does the same thing:conn.graceful_shutdown()andshutdown_initiated = true. From there the existing post-shutdown path is unchanged: the loop keeps surfacing the streams h2 still yields and returns when the connection is done.graceful_shutdown_connection()callsnotify_one(), which leaves a permit if the loop is not waiting at that moment.Compatibility
No existing signature changes. Without a call to the new method the added branch is never woken, so the accept loop behaves as before.
Tests
In
protocols/http/v2/mod.rs, next to the existing graceful shutdown tests, using a raw h2 codec client over an in-memory duplex stream:test_graceful_shutdown_connection_drains_connection: the session of stream 1 callsgraceful_shutdown_connection()while stream 1 is in flight; no shutdown signal is sent. The client acknowledges the PING and receivesGOAWAY(last_stream_id = MAX)and thenGOAWAY(last_stream_id = 1), bothNO_ERROR. Stream 1 is answered only after the final GOAWAY and completes. A stream 3 opened after that is not surfaced, and the accept loop returns and closes the connection.test_connection_stays_open_without_graceful_shutdown_connection: without the call, two streams on the same connection are answered and no GOAWAY is sent.test_graceful_shutdown_connection_without_accept_loop: a session fromHttpSession::from_h2_connreturnsfalse.In
protocols/http/server.rs:graceful_shutdown_connection_disables_h1_keepalive: on HTTP/1.1 the call returnstrueand keepalive is off afterwards.graceful_shutdown_connection_is_noop_for_custom_session: returnsfalse.Checks run locally (Debian 12, x86_64), following
.github/workflows/build.yml:cargo +1.97.1 fmt --all -- --check: cleancargo +1.97.1 clippy --all-targets --all -- --allow=unknown-lints --deny=warnings: cleancargo +1.97.1 test -p pingora-core: all pass excepttest_h2_late_frames_on_cancelled_streams_keep_connection, which also fails onmainhere and is skipped in CIcargo +1.85.0 check -p pingora-core: cleanI did not run the pingora-proxy test suite.
🤖 Generated with Claude Code