Giter Site home page Giter Site logo

Server starts and stops immediately when both enableVirtualThreads and enableAdminVirtualThreads are set to true about dropwizard HOT 5 CLOSED

ochedru avatar ochedru commented on May 26, 2024 1
Server starts and stops immediately when both enableVirtualThreads and enableAdminVirtualThreads are set to true

from dropwizard.

Comments (5)

zUniQueX avatar zUniQueX commented on May 26, 2024 1

@ochedru Thanks for reporting this!

I've also reproduced the bug with the SimpleServerFactory and enableVirtualThreads set to true.

from dropwizard.

zUniQueX avatar zUniQueX commented on May 26, 2024 1

@cty123 The issue exists due to the presence of the enableVirtualThreads flag. This flag is present in the versions 3.x, 4.x and 5.x. After we have a solution, this will be merged to all those branches.

from dropwizard.

zUniQueX avatar zUniQueX commented on May 26, 2024

When using virtual threads, an AsynchronousCloseException is thrown to Jetty's AbstractConnector class while waiting to accept connections. When overriding the Dropwizard application's main method and calling Server.join() there, the issue doesn't occur. So it seems like the thread pool gets closed when the connection handling isn't done by blocking platform threads.

I'll investigate, if we can/should call Server.join() on the Dropwizard side or if we shouldn't do this because it happens inside the CLI execution.

from dropwizard.

cty123 avatar cty123 commented on May 26, 2024

I had the same issue, and here is what I have found out. Typically this is how the main function of a Dropwizard application looks like,

public static void main(String[] args) throws Exception {
    new ExampleApplication().run(args);
}

such that the main thread exits right after starting the application. This was fine before because the JVM wouldn't exit until all non-daemon threads have exited, which would never happen as there were admin thread pool executor as well as application executor executor each with non-daemon threads in the thread pool. (doc)

Platform threads are designated daemon or non-daemon threads. When the Java virtual machine starts up, there is usually one non-daemon thread (the thread that typically calls the application's main method). The shutdown sequence begins when all started non-daemon threads have terminated. Unstarted non-daemon threads do not prevent the shutdown sequence from beginning.

Starting with JDK21 together with the enablement of virtual threads, this is no longer the case, because virtual thread can not be a non-daemon thread(doc),

Tests if this thread is a daemon thread. The daemon status of a virtual thread is always true.

So what happens right now is that, when virtual thread is enabled for both admin and application connectors, the main thread is the only non-daemon thread(unless you spawn manually either in main or with dependency injection), such that when the main threads exits, the JVM immediately detects the termination of all non-daemon threads, and choose to shutdown, ignoring the virtual thread executor.

The solution I had was that, since the main thread is the only non-daemon thread, I can just make sure it doesn't exist, unless certain things happens like interrupt signal, so I just let the main thread to wait for itself,

public static void main(String[] args) throws Exception {
    new ExampleApplication().run(args);
    
    Thread.currentThread().join();
}

Ideally, I would like the run function to return a future or run blocking until completion, like,

public static void main(String[] args) throws Exception {
    new ExampleApplication().run(args).wait();
}

PS: Please let me know if there's better way to solve this.

from dropwizard.

cty123 avatar cty123 commented on May 26, 2024

Btw, this issue also exists in DW3 as I just ran into a few weeks before, I believe it also exists in DW4, but I haven't had a chance to reproduce on it.

from dropwizard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.