Skip to content

Connector#shutdown() gets stuck if Connector#close() is called with HTTP/2 clients. #13602

Closed as not planned

Description

@jacob-pro

Jetty version(s)

12.1.1

Java version/vendor

JDK 21

Description

The future returned by calling Connector#shutdown() will never complete if Connector#close() is later called whilst there are HTTP/2 clients connected.

Use Case

I need the application to support a graceful shutdown using Connector#shutdown(), however if this is taking too long then the application can be interrupted for a faster shutdown, calling Connector#close().

How to reproduce?

public static void main(String[] args) throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(), new HTTP2CServerConnectionFactory());
    connector.setPort(0);
    connector.setShutdownIdleTimeout(15_000);
    server.setConnectors(new Connector[] {connector});
    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();

    ContentResponse response = http2Client.newRequest(url)
        .method(HttpMethod.GET)
        .send();
    log.info("Response {}", response);

    // This future will wait for all connections to close
    CompletableFuture<Void> shutdownFuture = server.getConnectors()[0].shutdown();
    
    Thread.sleep(Duration.ofSeconds(1));

    // This should forcibly close connections
    server.getConnectors()[0].stop();

    log.info("Waiting for shutdown future...");
    shutdownFuture.get();
    log.info("Shutdown completed");
    System.exit(0);
}

Output:

13:43:26.771 [main] INFO  org.eclipse.jetty.server.Server - jetty-12.1.1; built: 2025-09-08T04:29:45.457Z; git: b7068950f9afa5f1df80e46053eda1d982895b03; jvm 21.0.7+6-LTS
13:43:26.805 [main] INFO  o.e.jetty.server.AbstractConnector - Started oejs.ServerConnector@63475ace{HTTP/1.1, (http/1.1, h2c)}{0.0.0.0:34647}
13:43:26.812 [main] INFO  org.eclipse.jetty.server.Server - Started oejs.Server@71e9ddb4{STARTING}[12.1.1,sto=0] @598ms
13:43:27.140 [main] INFO  org.example.ShutdownStuck - Response HttpContentResponse[HTTP/2.0 404 null - 413 bytes]
13:43:28.145 [main] INFO  o.e.jetty.server.AbstractConnector - Stopped oejs.ServerConnector@63475ace{HTTP/1.1, (http/1.1, h2c)}{0.0.0.0:0}
13:43:28.145 [main] INFO  org.example.ShutdownStuck - Waiting for shutdown future...

(Note if the client is swapped to HTTP/1.1 then the future returns as expected)

Suspected cause

The _shutdown variable is set to null before the HTTP/2 endpoints finish disconnecting:

Image Image

Contrast this with HTTP/1.1, when onEndPointClosed() the _shutdown variable is still present

Image

I'm not sure however why the timing of HTTP/1.1 and HTTP/2 endpoint disconnection differs though?

Activity

  1. added
    Bug
    on Sep 19, 2025
  2. serhiy-bzhezytskyy commented on Jul 23, 2026

    @serhiy-bzhezytskyy

    Root cause: doStop() clears _shutdown before the asynchronous HTTP/2 endpoint disconnect fires onEndPointClosed(), so the final Shutdown.check() is skipped and the shutdown() future never completes. HTTP/1.1 disconnects synchronously enough to avoid the window, which matches your observation that swapping to HTTP/1.1 returns the future as expected — thanks for the precise repro and the pointer to the _shutdown line, that's exactly it.

    Opened #15473 with a fix: isShutdownDone() treats a no-longer-running connector as done, and doStop() completes the pending shutdown before clearing _shutdown. Test covers shutdown()-then-stop() including with a still-active stream.

    Related: #13569 / #15368 (same graceful-shutdown area).

  3. lorban commented on Aug 21, 2026

    @lorban
    Contributor

    @jacob-pro There might be a bug lurking around (I haven't looked at the shutdown() code yet) but it looks like the problem here is that you're mis-using the API.

    The following adaptation of your reproducer seems to work fine:

    public static void main(String[] args) throws Exception {
    {
        Server server = new Server();
        ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(), new HTTP2CServerConnectionFactory());
        connector.setPort(0);
        connector.setShutdownIdleTimeout(15_000);
        server.setConnectors(new Connector[] {connector});
        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();
    
        ContentResponse response = http2Client.newRequest(url)
            .method(HttpMethod.GET)
            .send();
        log.info("Response {}", response);
    
        // This thread will wait for all connections to close
        Thread thread = new Thread(() -> LifeCycle.stop(connector));
        thread.start();
    
        Thread.sleep(1000);
    
        // This should forcibly close connections
        ((NetworkConnector)server.getConnectors()[0]).close();
    
        log.info("Waiting for shutdown thread...");
        thread.join();
        log.info("Shutdown completed");
        System.exit(0);
    }

    Could you please give it a try and let us know if the above does what you expect?

  4. self-assigned this
    on Aug 21, 2026
  5. lorban commented on Sep 11, 2026

    @lorban
    Contributor

    Graceful.shutdown() is not meant to be called directly, see: #15473 (comment)

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