Skip to content

fix: retire HTTP/2 client connections at stream ID exhaustion - #1667

Open
He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:fix/http2-client-stream-id-exhaustion
Open

He-Pin wants to merge 1 commit into
apache:mainfrom
He-Pin:fix/http2-client-stream-id-exhaustion

Conversation

@He-Pin

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

Copy link
Copy Markdown
Member

Motivation

HTTP/2 stream identifiers cannot be reused. A client connection that uses its last valid request stream identifier should stop accepting requests without discarding responses already assigned to it.

Modification

  • Keep each physical connection's stream identifiers monotonic. After sending the final request, cancel the direct flow's request input with StreamIdExhaustedException, which callers can observe with watchTermination. Let already assigned responses drain under the existing completion timeout.
  • Retire exhausted managed connections and carry only unassigned requests to a replacement. Do not replay already assigned requests.

Result

The final valid request and its response are handled normally. Direct callers can distinguish request-side retirement from a response failure, and managed callers can continue on a fresh connection. Incoming GOAWAY and the opt-in graceful GOAWAY behavior are not changed by this PR.

Tests

JDK 17: six focused tests passed on each of Scala 2.13.18 and 3.3.9. MiMa, formatting, and the Java API usage sample compilation passed. Full CI was not run locally.

References

Motivation:
A long-lived HTTP/2 client cannot reuse stream IDs. An unbounded Int
increment can wrap after allocating the final positive odd identifier.

Modification:
Stop request admission after rendering the last valid stream. Report
exhaustion to a direct client's request-side termination watcher while
letting the response half drain normally. Retire managed connections
after the last assignment and retain the buffered request if its source
completes while the replacement connection is being established.

Result:
Already assigned requests drain without replay. A direct client can
observe the exhaustion without losing responses; a managed client hands
only unassigned work to the replacement connection.

References:
RFC 9113 §5.1.1: https://www.rfc-editor.org/rfc/rfc9113.html#section-5.1.1
@He-Pin
He-Pin force-pushed the fix/http2-client-stream-id-exhaustion branch from ec6a834 to 64d809e Compare November 26, 2026 17:01

@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 allocator and the new rendering stage look correct to me, and the direct-connection path ends in the demux's existing request-side completion. A few points on the code:

  • Connecting.onUpstreamFinish is a fix of its own. On main Connecting inherits completeStage(), so a request sitting in the inlet is dropped if the source completes while any connection attempt is pending, including the first one. This PR fixes that, which is good, but it is not limited to stream ID exhaustion. The same hole remains in Unconnected: after a failed attempt onFailed switches to Unconnected and schedules the backoff, and Unconnected still has the default onUpstreamFinish, so a buffered request is lost if the source completes during the backoff. Could that be closed here as well, with a test?
  • Two counters have to stay in step. remainingRequests in PersistentConnection assumes exactly one stream ID per dispatched request and retires one step before ClientRequestRendering would cancel. That holds today because retire() completes requestOut first, so the later cancel is ignored. If the counts ever diverged, the cancel would reach onDownstreamFinish -> onDisconnected() and every in-flight request would get a 502. Would it be more robust for the Connected requestOut handler to treat a StreamIdExhaustedException cause as a retire, instead of or as well as the counter?
  • ClientStreamIds and ClientRequestRendering need @InternalApi and the INTERNAL API scaladoc, like the classes around them.
  • In onConnected, else if (isClosed(requestIn)) requestOut.complete() looks unreachable now: with the new Connecting.onUpstreamFinish, closed-and-not-available has already completed the stage.

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