Skip to content

Fix #15368 - HTTP/2 client aborts a fully-received response during graceful shutdown - #15435

Closed
serhiy-bzhezytskyy wants to merge 3 commits into
jetty:jetty-12.1.xfrom
serhiy-bzhezytskyy:fix/15368-http2-graceful-close-abort
Closed

serhiy-bzhezytskyy wants to merge 3 commits into
jetty:jetty-12.1.xfrom
serhiy-bzhezytskyy:fix/15368-http2-graceful-close-abort

Conversation

@serhiy-bzhezytskyy

Copy link
Copy Markdown

Fixes #15368.

HttpConnectionOverHTTP2.ensureClosedOrAbortActiveChannels() aborts every active channel unconditionally when the connection is closed. When the connection closes at the end of a server graceful shutdown (GOAWAY), a channel whose response has been fully received but not yet consumed by the application is still active, so it gets aborted and the already-received response is discarded with a ClosedChannelException.

Fix: skip aborting a channel whose last DATA frame has already been received (HttpChannelOverHTTP2.isLastDataReceived()) — that response is complete and must be delivered to the application. Requests still in flight are aborted as before, so the original intent of the abort-on-close change is preserved.

Gating on HttpExchange.isResponseComplete() does not work here, because HTTP/2 content is consumed lazily and the exchange is not marked COMPLETED until the application reads the body; isLastDataReceived() reflects that the response bytes have arrived off the wire.

Test: HttpClientTransportOverHTTP2Test.testConnectionCloseDoesNotAbortExchangeWithCompletedResponse — a white-box test that captures the HttpConnectionOverHTTP2, lets a response be fully received but not consumed, then calls connection.close() directly (no socket-timing dependence). It fails with ClosedChannelException at ensureClosedOrAbortActiveChannels without the fix and passes with it. GoAwayTest and the rest of HttpClientTransportOverHTTP2Test still pass.

This was reproduced independently from the Apache Solr side (TestGracefulJettyShutdown, ~60% failure on 12.1.10), as discussed in #15368.

…ng graceful shutdown

HttpConnectionOverHTTP2.ensureClosedOrAbortActiveChannels() aborts every active
channel unconditionally on connection close. When a connection is closed at the end
of a server graceful shutdown (GOAWAY), a channel whose response has been fully
received but not yet consumed by the application is still active, so it gets aborted
and the already-received response is discarded with a ClosedChannelException.

Skip aborting a channel whose last DATA frame has already been received
(HttpChannelOverHTTP2.isLastDataReceived()); such a response is complete and must be
delivered to the application. Requests still in flight are aborted as before.

Regression from the abort-on-close change; reproduced independently from Apache Solr
(TestGracefulJettyShutdown) on 12.1.10.

Signed-off-by: serhiy-bzhezytskyy <me@serhiy-bzhezytskyy.com>
@serhiy-bzhezytskyy

Copy link
Copy Markdown
Author

The one red check is HttpClientIdleTimeoutTest.testRequestIdleTimeout[5] H3_QUICHE. That's a known flaky test — #14901 tracks it (labeled flaky-test, ~20% failure rate on the H3_QUICHE transport). It's unrelated to this change: it's in the jetty-test-client-transports module on the HTTP/3 transport, while this PR only touches jetty-http2-client-transport. I reproduced it locally and it fails identically on a clean jetty-12.1.x with this change reverted (same line 128, same ConditionTimeout), so it's pre-existing rather than caused by the fix.

@lorban
lorban requested review from lorban and sbordet July 17, 2026 12:16

@lorban lorban left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Apart from a few minor changes in the test, this looks like a decent fix based on a proper analysis.

@sbordet am I missing something?

Comment on lines +167 to +175
// White-box reproduction of jetty#15368. The race is: the response has been fully received
// by the client transport (isResponseComplete() == true), but the exchange has not yet
// terminated (the application has not consumed the body), so the channel is still "active".
// If the connection is closed in this window (e.g. a GOAWAY at the end of a server graceful
// shutdown), the connection must NOT abort the exchange, otherwise the already-received
// response is discarded with a ClosedChannelException.
//
// To hit this window deterministically, we capture the HttpConnectionOverHTTP2 instance, let
// a small response be fully received (but not consumed), then invoke connection.close()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please remove this comment as the git history should be all the context needed to dive back into this test.


HTTP2CServerConnectionFactory h2c = new HTTP2CServerConnectionFactory(new HttpConfiguration());
h2c.setInitialSessionRecvWindow(FlowControlStrategy.DEFAULT_WINDOW_SIZE);
h2c.setInitialStreamRecvWindow(FlowControlStrategy.DEFAULT_WINDOW_SIZE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is setting the receive windows necessary? What's the reason?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not necessary — removed. They were setting the windows back to the default (DEFAULT_WINDOW_SIZE), and the test still reproduces the race and passes without them.

@Override
public boolean handle(Request request, org.eclipse.jetty.server.Response response, Callback callback)
{
response.setStatus(HttpStatus.OK_200);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

200 OK is the default status, there is no need to repeat it.


// The already-received response must still be deliverable.
byte[] body = IO.readBytes(listener.getInputStream());
assertEquals(payload.length, body.length);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You should assert that the array contents are equal too with assertArrayEquals(payload, body).

- Remove the explanatory comment block (git history carries the context).
- Drop the redundant server recv-window settings (not needed to hit the race)
  and the explicit 200 status (it is the default).
- Assert the full response body with assertArrayEquals.

Signed-off-by: Serhiy Bzhezytskyy <me@serhiy-bzhezytskyy.com>
@serhiy-bzhezytskyy

Copy link
Copy Markdown
Author

Thanks for the review! Addressed all four:

  • Removed the comment block.
  • Removed the setInitialSessionRecvWindow/setInitialStreamRecvWindow calls — you're right, they weren't needed; the test still reproduces the race and passes without them.
  • Dropped the redundant setStatus(200).
  • Switched to assertArrayEquals(payload, body).

@serhiy-bzhezytskyy

Copy link
Copy Markdown
Author

Hi Ludovic — I think I've addressed all four review comments (the extra comment block, the receive-window setting, the redundant 200 OK, and the assertArrayEquals), and CI is green across JDK 17–26. Happy to make any further changes if you'd like — otherwise it's ready whenever you are.

lorban
lorban previously approved these changes Jul 22, 2026

@lorban lorban left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM - @sbordet anything I missed?

@sbordet

sbordet commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

@lorban I have reservations about this change. The idea is that the method is supposed to really do a hard abort of all the streams, because we cannot wait forever that the application reads.

I have to dig into the issue to be sure -- it might be that the change is correct, but we need to ensure that when we want to dispose all the streams, we do even if there is data to be read.

Adds a no-leak assertion to the existing close() white-box test, and a
companion test on the onFailure() path (HTTP2Session.onConnectionFailure ->
connection.onFailure, which destroys without a trailing session.close()).
Both assert the fully-received response is still delivered AND the pool ends
leaked=0 with no lingering multiplexed connection. Both are non-vacuous:
reverting the isLastDataReceived() gate makes each fail on the body read.

Signed-off-by: serhiy-bzhezytskyy <me@serhiy-bzhezytskyy.com>
@serhiy-bzhezytskyy

Copy link
Copy Markdown
Author

Thanks Ludovic and Simone.

Simone — on the concern that we must still hard-abort/dispose the streams even when there is data to be read: I don't think this change weakens that. From bf5e8a40de6 (#15134 / #14692 / #14654) the guarantees were "clear activeChannels and abort active requests." Both still hold — activeChannels.clear() runs unconditionally (I only gated the abort() call), and in-flight requests (!isLastDataReceived()) are still aborted. The only ones skipped are those whose last DATA frame has already arrived, so the full response is already in the client's buffers.

Such a stream is already CLOSED, not just active: on the client, sending the request end-stream takes it to LOCALLY_CLOSED, then receiving the response end-stream runs updateCloseAfterReceived() → doClose() → closeState = CLOSED. So it's disposed by state, and consuming the buffered response drives the normal release/destroy.

That's also why this doesn't diverge from the hard-abort path. HTTP2Session.onSessionFailure does failStreams(stream -> true, ...), but failStreams skips stream.isClosed() first, so a fully-received stream is already excluded there too. And on graceful half-close the session already uses the same predicate explicitly:

// Since nothing else will arrive from the other peer, reset
// the streams for which the other peer did not send all frames.
Predicate<Stream> failIf = stream -> !((HTTP2Stream)stream).isLastDataReceived();

so gating on isLastDataReceived() at the connection layer is what the session layer already does.

I pushed fe9149e to cover disposal on both callers of ensureClosedOrAbortActiveChannels: close() (graceful shutdown), and onFailure() — the stricter one, since it destroys without a trailing session.close(). That test fails the session via onConnectionFailure(...) while the completed-but-unconsumed exchange is active, then asserts the body is still readable and the pool ends leaked=0. GoAwayTest.testImmediateGoAwayAndDisconnectDoesNotLeakClientConnections (the 1000-connection leak test) also still passes, since those requests are in-flight and still aborted.

If you'd rather gate it differently or see a hole in the onFailure path, happy to adjust.

@janhoy

janhoy commented Jul 28, 2026 •

Copy link
Copy Markdown

@sbordet Gentle ping to ask for progress in reviewing/merging this. On the Solr side we're currently blocked from upgrading Jetty...

@lorban

lorban commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

@janhoy sorry for the delay, we've been busier that usual recently so work on this issue and the related ones was unfortunately put to the back burner. As a result, fixing this can't happen in the upcoming Jetty release that is due for the coming days.

We want to fully review all issues related to #15368 and make sure this is all fixed once for good. Hopefully, this will happen before the next-next release; I've marked the issue as high-priority to make sure this won't escape our attention.

We'll keep you posted as we progress on this.

@lorban

lorban commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Superseded by #15628

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

HTTP/2 client aborts exchange and loses response completed during server graceful shutdown (regression in 12.1.10 / 12.0.36 from #15134 / #14692)

4 participants