Skip to content

Graceful shutdown continues to allow new requests in HTTP/2 #13569

Closed as not planned

Description

@jacob-pro

Jetty version(s)

Jetty 12.1.1

Jetty Environment

ee10

Java version/vendor (use: java -version)

JDK 21

Description

When performing a graceful shutdown and using HTTP/2:

  • New requests seem to continue being accepted on existing connections
  • The graceful shutdown does not complete until the connections themselves timeout.

I was expecting this to have equivalent behaviour to HTTP/1

  • New requests should be rejected
  • The graceful shutdown should complete as soon as all active requests complete.

How to reproduce?

    public static class Http2GracefulStopServer {
        public static void main(String[] args) throws Exception {
            Server server = new Server();
            ServerConnector connector = new ServerConnector(server, new HTTP2CServerConnectionFactory());
            connector.setPort(0);
            connector.setShutdownIdleTimeout(15_000);
            server.setConnectors(new Connector[] {connector});
            ServletContextHandler context = new ServletContextHandler();
            CountDownLatch didRecvRequest = new CountDownLatch(1);
            CountDownLatch shouldFinishResponse = new CountDownLatch(1);
            context.addServlet(new HttpServlet() {
                @SneakyThrows
                @Override
                protected void service(HttpServletRequest req, HttpServletResponse resp) {
                    log.info("Received request: {}", req);
                    didRecvRequest.countDown();
                    resp.getOutputStream().flush();
                    shouldFinishResponse.await();
                }
            }, "/*");
            server.setHandler(context);
            server.start();
            String url = "http://localhost:%d/".formatted(connector.getLocalPort());

            ClientConnector clientConnector = new ClientConnector();
            HttpClient http2Client = new HttpClient(new HttpClientTransportDynamic(clientConnector, new ClientConnectionFactoryOverHTTP2.HTTP2(new HTTP2Client(clientConnector))));
            http2Client.start();

            http2Client.newRequest(url)
                .method(HttpMethod.GET)
                .send(resp -> log.info("First request finished {}", resp.getResponse().getStatus()));

            didRecvRequest.await();

            CompletableFuture<Void> shutdownCompleted = server.getConnectors()[0].shutdown();

            // Why does this one succeed ???
            http2Client.newRequest(url)
                .method(HttpMethod.GET)
                .send(resp -> log.info("Second request finished {}", resp.getResponse().getStatus()));

            shouldFinishResponse.countDown();
            Instant startTime =  Instant.now();

            shutdownCompleted.get();
            // Why does this take 15 seconds / shutdownIdleTimeout ???
            log.info("Shutdown completed after {}", Duration.between(startTime, Instant.now()));
        }
    }

Activity

  1. added
    Bug
    on Sep 8, 2025
  2. jacob-pro commented on Sep 11, 2025

    @jacob-pro
    ContributorAuthor

    I realise I had a few misunderstandings about the equivalent HTTP/1.1 behaviour; in my own HTTP/1.1 testing the request after shutdown() fails because it is making a new connection, if I attempt to re-use an existing connection then it does succeed. However it also has a Connection: close header as per: #2749 preventing further requests.

    Adapting my HTTP/2 test above, though I can still send many requests after shutdown:

    CompletableFuture<Void> shutdownCompleted = server.getConnectors()[0].shutdown();
    
    for (int i = 0; i < 5; i++) {
        Thread.sleep(Duration.ofSeconds(1));
        ContentResponse resp = http2Client.newRequest(url)
            .method(HttpMethod.GET)
            .send();
        log.info("HTTP/2 call {} after shutdown succeeded: {}", i, resp);
    }

    I found I was able to add a HTTP/2 customizer to create equivalent behaviour to the HTTP/1.1 case:

    httpConfiguration.addCustomizer((request, mutable) -> {
        ConnectionMetaData metaData = request.getConnectionMetaData();
        if (metaData.getHttpVersion() == HttpVersion.HTTP_2 && metaData.getConnector().isShutdown()) {
            HTTP2ServerSession session = ((HTTP2ServerConnection) metaData).getSession();
            session.goAway(GoAwayFrame.GRACEFUL, Callback.NOOP);
        }
        return request;
    });

    This then allows 1 more HTTP/2 request after shutdown, but prevents additional requests. I wonder though if there is a way to send the GOAWAY immediately at the point of shutdown()?

    I found this PR which looks like it was supposed to implement GOAWAY on shutdown, I wonder if it is not working correctly @sbordet? #4554

  3. serhiy-bzhezytskyy commented on Jul 21, 2026

    @serhiy-bzhezytskyy

    I ran into this while working on the related HTTP/2 graceful-shutdown bug #15368 (my fix: #15435) and dug into this one too. Root cause looks like: connector.shutdown() stops the acceptors and reduces the endpoint idle timeouts, but it never reaches the SessionContainer graceful path added in #4554 — so live HTTP/2 sessions never get a GOAWAY, keep accepting new streams, and the shutdown only completes at the shutdown idle timeout (the 15s the reporter saw). Graceful.shutdown(connector) works because it traverses and finds SessionContainer; the direct connector.shutdown() call doesn't.

    That seems to violate the Connector/Graceful contract ("should not accept any new load … completed once all load is completed"), so I put together a fix + a reproducing test:

    • Test first (red): connector.shutdown() → the client session never receives a GOAWAY.
    • Fix: have AbstractConnector.shutdown() also shut down its contained Graceful beans (i.e. SessionContainer) — only the contained ones, since the connector is itself Graceful and would otherwise recurse. Side-effect-only; I kept shutdown()'s completion semantics unchanged.

    One flag from my own testing: my first attempt also folded the graceful futures into the returned CompletableFuture, which regressed HTTP/1.1 pipelining (ServerConnectorHttpServerTest.testShutdown). Keeping the return as-is and treating the graceful close as a pure side effect fixes this with the full jetty-server suite still green.

    Before I open a PR — is AbstractConnector the right place, or would you prefer it elsewhere (or is Graceful.shutdown(connector) the intended API and connector.shutdown() meant to be lighter)? Test + fix ready whenever. @sbordet

  4. serhiy-bzhezytskyy commented on Jul 23, 2026

    @serhiy-bzhezytskyy

    I ran into this while working on #15368 (the HTTP/2 client-side graceful-shutdown bug, #15435) and dug into this one too. Root cause: AbstractConnector.shutdown() never reaches the HTTP/2 SessionContainer's graceful path (from #4554), so connector.shutdown() sends no GOAWAY and the session keeps accepting streams until the idle timeout — the Graceful "no new load" contract isn't honored for HTTP/2.

    I have a fix + test ready (shut down the connector's contained Graceful beans, keeping shutdown() completion gated on acceptors/endpoints as before). Opened #15472 — happy to adjust the approach or hand it off if you'd prefer to take it a different way.

    Related: #15368/#15435 (client side of the same graceful-shutdown behavior), #13602 (a separate shutdown()-then-stop() hang in the same area).

  5. lorban commented on Aug 21, 2026

    @lorban
    Contributor

    This seems similar to #13602: you should not call Connector.shutdown() but Connector.stop() instead.

    Here following sample seems to do what you're looking for:

    public static void main(String[] args) throws Exception
    {
        Server server = new Server();
        ServerConnector connector = new ServerConnector(server, new HTTP2CServerConnectionFactory());
        connector.setPort(0);
        connector.setShutdownIdleTimeout(15_000);
        server.setConnectors(new Connector[]{connector});
        CountDownLatch didRecvRequest = new CountDownLatch(1);
        CountDownLatch shouldFinishResponse = new CountDownLatch(1);
        server.setHandler(new Handler.Abstract()
        {
            @Override
            public boolean handle(Request request, Response response, Callback callback)
            {
                log.info("Received request: {}", request);
                didRecvRequest.countDown();
                response.write(false, null, Callback.from(() ->
                {
                    try
                    {
                        shouldFinishResponse.await();
                    }
                    catch (InterruptedException e)
                    {
                        throw new RuntimeException(e);
                    }
                }, callback));
                return true;
            }
        });
        server.start();
        String url = "http://localhost:%d/".formatted(connector.getLocalPort());
    
        ClientConnector clientConnector = new ClientConnector();
        HttpClient http2Client = new HttpClient(new HttpClientTransportDynamic(clientConnector, new ClientConnectionFactoryOverHTTP2.HTTP2(new HTTP2Client(clientConnector))));
        http2Client.start();
    
        http2Client.newRequest(url)
            .method(HttpMethod.GET)
            .send(resp -> log.info("First request finished {}", resp.getResponse().getStatus()));
    
        didRecvRequest.await();
    
        Thread thread = new Thread(() -> LifeCycle.stop(connector));
        thread.start();
    
        for (int i = 0; i < 5; i++) {
            try
            {
                Thread.sleep(1000);
                ContentResponse resp = http2Client.newRequest(url)
                    .method(HttpMethod.GET)
                    .send();
                log.info("HTTP/2 call {} after shutdown succeeded: {}", i, resp);
            }
            catch (Exception e)
            {
                log.info("HTTP/2 call {} after shutdown failed: {}", i, e.toString());
            }
        }
        shouldFinishResponse.countDown();
        Instant startTime = Instant.now();
    
        thread.join();
        // Why does this take 15 seconds / shutdownIdleTimeout ???
        log.info("Shutdown completed after {}", Duration.between(startTime, Instant.now()));
    
        http2Client.stop();
        server.stop();
    }

    Did I miss anything?

  6. self-assigned this
    on Aug 21, 2026
  7. serhiy-bzhezytskyy commented on Aug 31, 2026

    @serhiy-bzhezytskyy

    @lorban — you're right that stop() does what both reporters were reaching for, and I'm not arguing that these two PRs should land as they are. I'd rather ask whether they should stay open at all.

    What I can add is a measurement on current jetty-12.1.x, with only the tests applied and the production change reverted:

    So both still reproduce. If shutdown() is meant to honour the Graceful contract for HTTP/2, the defects are still there. If it isn't — if stop() is simply the API and shutdown() was never intended to reach the session — then these are working-as-intended and I'll close #15472 and #15473.

    Two things on my own side, for accuracy. Of the three tests, testConnectorShutdownThenStopCompletesFutureOnHTTP2 now passes without the production change, so it no longer proves anything and would come out either way. And neither PR overlaps #15628: both touch jetty-server's AbstractConnector, not the HTTP/2 client transport.

    Either answer works for me — I'd just rather not leave two PRs open if the premise is wrong.

  8. lorban commented on Aug 31, 2026

    @lorban
    Contributor

    @serhiy-bzhezytskyy Graceful.shutdown() is not exactly meant to be called directly: all implementations are meant to perform the necessary cleanup for the component upon which the call is performed, and it's up to the caller to arrange for calling shutdown() on every necessary component in the right order. In a nutshell, this is an implementation detail of some LifeCycle.stop() implementations.

    So if calling Server.stop() or Connector.stop() does send GOAWAY and waits the appropriate time, then everything works as intended.

    That doesn't mean we can't improve a thing or two in that area, including in the field of docs.

  9. serhiy-bzhezytskyy commented on Aug 31, 2026

    @serhiy-bzhezytskyy

    Thanks, I see. #15472/#15473 closed.

  10. lorban commented on Sep 11, 2026

    @lorban
    Contributor

    Graceful.shutdown() is not meant to be called directly, closing as misuse.

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

Metadata

Metadata

Assignees

Labels

Bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions