Skip to content

feat: send GOAWAY on opt-in HTTP/2 client graceful close - #1665

Open
He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:feat/http2-client-graceful-goaway
Open

He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:feat/http2-client-graceful-goaway

Conversation

@He-Pin

@He-Pin He-Pin commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Motivation

Let a direct HTTP/2 client notify the peer when the application has finished sending requests, without changing defaults or the existing response drain.

Modification

  • Add the opt-in pekko.http.client.http2.send-goaway-on-graceful-close. Queue one GOAWAY(NO_ERROR) on normal request-source completion and preserve outstanding responses and their completion deadline.
  • Keep existing cancellation and failure behavior; source failure does not trigger a graceful GOAWAY. Managed and pooled connections do not use the option.
  • Store the flag in a separate immutable client settings field. Keep the released JVM constructor, factory and copy signatures; do not claim that the package-internal case-class Product arity or Scala 2 extractor payload shape is unchanged.

Result

Opted-in direct connections close gracefully without losing accepted responses, and applications without the opt-in keep their existing behavior.

Tests

  • JDK 17 and Scala 2.13.18 / 3.3.9: 10 focused settings and direct/managed lifecycle tests passed on each, including managed cancellation with the setting on and an ordinary request-source failure.
  • A Java settings caller and Scala consumers compiled against the published 2.0.0 artifacts passed on both axes. Scoped settings MiMa, formatting and whitespace checks passed. Full CI has not been run on this revision.

References

@He-Pin
He-Pin force-pushed the feat/http2-client-graceful-goaway branch from 927b283 to beec719 Compare November 26, 2026 16:45
@He-Pin
He-Pin requested a review from pjfanning November 26, 2026 16:47

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The engine change looks right to me: the GOAWAY goes through the control-frame queue after any request HEADERS, in-flight streams still drain, and nothing changes with the option off. Two points on the code:

The setting is stored by wrapping internalSettings. GracefulGoAwayEnabled(delegate) wraps the slot, sendGoAwayOnGracefulClose is true when the slot holds the wrapper, and withInternalSettings re-wraps to keep the flag. This is the same concern as on #981, #1556, #1589 and now #1664, each of which uses the slot in a different way:

  • settings.internalSettings no longer returns what was passed to withInternalSettings once the flag is on, so any reader has to know to unwrap it.
  • copy(internalSettings = Some(x)) on the impl silently drops the flag.
  • It does not compose with the other PRs: each would overwrite, or fail to recognise, the others' value in the slot.

I would rather this were a real field on Http2ClientSettingsImpl, and that we settle one approach for all of these PRs.

withoutClientGoAway has no test. It is the only thing that stops managedPersistentHttp2* from sending the GOAWAY when the wrapper completes its inner request source on retirement or downstream cancellation, and removing the two withoutClientGoAway. prefixes would leave every test green. Could you add a case with the option on and a managed connection, asserting that no GOAWAY is sent on cancel or retire?

Smaller: a request source that fails rather than completes is described as not sending a GOAWAY, and by my reading that is what happens (onUpstreamFailure -> failStage), but there is no user.requestOut.sendError(...) case to pin it.

Motivation:
Allow direct HTTP/2 clients to announce that they have stopped
sending requests before closing an otherwise healthy connection.

Modification:
Queue a single GOAWAY(NO_ERROR) on a normal request-source completion
when the option is enabled. Keep the existing response drain and do
not change managed cancellation or application-failure behavior.
Store the opt-in in an independent immutable settings field; preserve
released constructor, factory and copy descriptors.

Result:
Existing applications keep the default and error behavior. A caller
can opt into graceful close without replacing the internal settings,
and an older settings copy preserves the active opt-in.

Tests:
JDK 17 on Scala 2.13.18 and 3.3.9: ten focused settings, direct and
managed lifecycle cases per axis, an actual Java settings caller,
released 2.0.0 Scala linkage probes and zero scoped MiMa findings.
Scoped Scala formatting and whitespace checks passed.

References:
RFC 9113 Section 6.8. Addresses PJ's review on apache#1665; related apache#1556.

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushGOAWAY in Http2Demux is also rewritten by #1664, which routes every GOAWAY through sendFinalGoAway -> freezeAdmissionBoundary() -> lastStreamId(). Whichever lands second has to keep goAwayLastStreamId(), otherwise a post-graceful error GOAWAY advertises the client's own last request id instead of 0 (the "not raise the GOAWAY boundary if keepalive later times out" test would catch it).

Java: the new javadsl Http2ClientSettings.withSendGoAwayOnGracefulClose cannot be applied to a connection from Java, because javadsl ClientConnectionSettings has no withHttp2Settings/mapHttp2Settings (the docs fall back to a config string for Java). That gap is pre-existing, but it means the new setter is effectively config-only for Java users.

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.

2 participants