Skip to content

Implement two-phase graceful shutdown for HTTP/2 server connections, as recommended by RFC 9113 section 6.8. - #743

Open
arturobernalg wants to merge 1 commit into
apache:masterfrom
arturobernalg:h2-graceful-double-goaway
Open

arturobernalg wants to merge 1 commit into
apache:masterfrom
arturobernalg:h2-graceful-double-goaway

Conversation

@arturobernalg

Copy link
Copy Markdown
Member

Implement two-phase graceful shutdown for HTTP/2 server connections, as recommended by RFC 9113 section 6.8.

The server first sends GOAWAY with the maximum stream ID, allowing requests already in transit to be processed. A PING exchange is then used to establish a round-trip interval before sending the final GOAWAY with the last accepted stream ID.

The implementation reuses the existing PING handling and timeout mechanisms without blocking the I/O reactor or introducing additional scheduling infrastructure.

Client-side shutdown behavior remains unchanged.

@arturobernalg
arturobernalg requested a review from ok2c November 10, 2026 16:05
@shinseongsu

Copy link
Copy Markdown
Contributor

Hi @arturobernalg, nice work on this one 👍 I tried it against a raw-socket H2 client and the main case works: a request that shows up after the first GOAWAY gets served and the final GOAWAY comes back with last=1. On master the same request is just dropped (last=0).

One thing I ran into: if the peer never answers the PING, the pingAckTimeout deadline doesn't seem to kick in. gracefulShutdownDeadlineReached() only runs from onInput() / onOutput(), so once the GOAWAY + PING are flushed, a silent peer never triggers another I/O event.

Setup: server with soTimeout=30s, client finishes the preface/SETTINGS and then goes quiet.

initiateShutdown() + awaitShutdown(60s):

  +10ms  GOAWAY last=2147483647
  +11ms  PING
  +30030ms GOAWAY last=0      <- comes from onTimeout (soTimeout), not the 5s probe

close(CloseMode.GRACEFUL):

  +10ms  GOAWAY last=2147483647
  +12ms  PING
  (no final GOAWAY, reactor force-closes the socket at 5005ms)

With the default IOReactorConfig (soTimeout=0), I think the first case would only end when the caller's await runs out.

Full matrix (master / this PR / local tweak)
peer behaviour master PR local tweak
ACKs the PING final GOAWAY 10ms 14ms 35ms
sends a request after GOAWAY(MAX) request dropped, last=0 served, last=1 served, last=1
never ACKs, initiateShutdown/await 10ms 30030ms 5007ms
never ACKs, close(GRACEFUL) 9ms no final GOAWAY, closed at 5005ms same as PR

To check it, I tried a small local change: while the probe is pending, set the session socket timeout to pingAckTimeout. In onTimeout(), if we're still waiting on the probe, send the final GOAWAY and restore the old timeout. Otherwise it does the normal timeout handling. With that the silent case ends at ~5s with GOAWAY last=0, and the ACK / in-flight cases behave the same as before.

// executeShutdown(), server branch
if (!pingAckTimeout.isDisabled()) {
    gracefulShutdownSavedSocketTimeout = ioSession.getSocketTimeout();
    ioSession.setSocketTimeout(pingAckTimeout);
}

// onTimeout()
if (gracefulShutdownAwaitingPing) {
    completeGracefulShutdown();   // also restores the saved socket timeout
    requestSessionOutput();
    return;
}

But I noticed the tests check setSocketTimeout is never called (testServerGracefulShutdownPreservesConfiguredSocketTimeout and 3 others), so I guess you avoided touching the socket timeout on purpose 🤔 If so, maybe something like a min(pingAckTimeout, configured timeout) while the probe is pending, or another way to wake the session, would work. Happy to open a PR against your branch with the tweak + a test if that helps.

There's also the close(GRACEFUL) case: the reactor gives up after 5s and pingAckTimeout is also 5s, so even with the tweak the final GOAWAY loses that race. Not sure that's worth handling here, just mentioning it.

@arturobernalg
arturobernalg force-pushed the h2-graceful-double-goaway branch from 3403ddf to 081878a Compare November 27, 2026 10:07
@arturobernalg

Copy link
Copy Markdown
Member Author

Good catch — you were right, this was a real bug. Pushed a fix: the GOAWAY probe now bounds itself by temporarily shortening the session timeout (capped at 1s) instead of relying on I/O events a silent peer never generates. Also added a real end-to-end test for this exact scenario, a raw-socket client that never ACKs the PING, covering both close(CloseMode.GRACEFUL) and initiateShutdown()/awaitShutdown() — green.

Could you retest against your raw-socket client when you get a chance?

Send an initial GOAWAY with the maximum stream ID, followed by
a PING to allow in-flight requests to arrive before sending the
final GOAWAY with the last accepted stream ID.

A silent peer never triggers onInput()/onOutput(), so the PING
ACK deadline was only ever checked opportunistically and never
fired on its own. Bound the probe by temporarily shortening the
session timeout to it (capped at 1s, well under the 5s grace an
I/O reactor gives a graceful shutdown before forcing it closed),
mirroring the same timeout swap/restore validateAfterInactivity
already uses for its own PING round trip. onTimeout() now tells
probe expiry (send the final GOAWAY) apart from a genuine dead
connection (unchanged hard-kill behavior).

Add a real-server integration test with a raw-socket client that
never ACKs the PING, covering both close(CloseMode.GRACEFUL) and
initiateShutdown()/awaitShutdown().
@arturobernalg
arturobernalg force-pushed the h2-graceful-double-goaway branch from 081878a to 0517824 Compare November 27, 2026 10:18
@shinseongsu

Copy link
Copy Markdown
Contributor

Retested 0517824 with the same raw-socket client and it looks good now 🎉 A silent peer gets the final GOAWAY after ~1s with both close(GRACEFUL) and initiateShutdown()/awaitShutdown(), including with the default soTimeout of 0. The ACK and in-flight request cases still work as before. I also tried a request whose body shows up 3s later without a PING ACK: the final GOAWAY goes out at ~1s with last=1, and the request still completes with a 200. Thanks for the quick fix!

@ok2c ok2c added the 5.6.x 5.6 release series label Nov 28, 2026
@ok2c ok2c added this to the 5.6-alpha1 milestone Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5.6.x 5.6 release series

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants