Skip to content

Let a session gracefully shut down its downstream HTTP/2 connection - #1126

Open
starjoe wants to merge 1 commit into
cloudflare:mainfrom
starjoe:h2-graceful-shutdown-connection
Open

starjoe wants to merge 1 commit into
cloudflare:mainfrom
starjoe:h2-graceful-shutdown-connection

Conversation

@starjoe

@starjoe starjoe commented Nov 17, 2026

Copy link
Copy Markdown

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) -> bool
  • server::Session::graceful_shutdown_connection(&mut self) -> bool

Semantics:

  • HTTP/2: the connection sends GOAWAY and accepts no new streams. Streams already in flight, including the calling one, are processed as usual, and the connection is closed once they are done. This is the drain accept_downstream_sessions already runs on a shutdown signal, limited to one connection. Other connections are not affected.
  • HTTP/1.x: same as set_keepalive(None); the connection is closed after the current response.
  • Returns false and does nothing for subrequest and custom sessions, and for HTTP/2 sessions not accepted by accept_downstream_sessions (for example, created with HttpSession::from_h2_conn). Returns true if the shutdown was requested, including when another stream of the same connection already requested it.

How

accept_downstream_sessions creates one tokio::sync::Notify per connection and stores a clone of the Arc in each HttpSession it accepts (a new private field, None for sessions created elsewhere). The select! in the accept loop gets one more branch, right after the shutdown-signal branch, that does the same thing: conn.graceful_shutdown() and shutdown_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() calls notify_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 calls graceful_shutdown_connection() while stream 1 is in flight; no shutdown signal is sent. The client acknowledges the PING and receives GOAWAY(last_stream_id = MAX) and then GOAWAY(last_stream_id = 1), both NO_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 from HttpSession::from_h2_conn returns false.

In protocols/http/server.rs:

  • graceful_shutdown_connection_disables_h1_keepalive: on HTTP/1.1 the call returns true and keepalive is off afterwards.
  • graceful_shutdown_connection_is_noop_for_custom_session: returns false.

Checks run locally (Debian 12, x86_64), following .github/workflows/build.yml:

  • cargo +1.97.1 fmt --all -- --check: clean
  • cargo +1.97.1 clippy --all-targets --all -- --allow=unknown-lints --deny=warnings: clean
  • cargo +1.97.1 test -p pingora-core: all pass except test_h2_late_frames_on_cancelled_streams_keep_connection, which also fails on main here and is skipped in CI
  • cargo +1.85.0 check -p pingora-core: clean

I did not run the pingora-proxy test suite.

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let an application gracefully shut down (GOAWAY) a single downstream HTTP/2 connection

1 participant