Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JVM stops right after deploying virtual verticle #5052

Open
tsegismont opened this issue Jan 5, 2024 · 6 comments
Open

JVM stops right after deploying virtual verticle #5052

tsegismont opened this issue Jan 5, 2024 · 6 comments
Labels
Milestone

Comments

@tsegismont
Copy link
Contributor

Consider this application:

public class MainVerticle extends AbstractVerticle {

  public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    DeploymentOptions deploymentOptions = new DeploymentOptions()
      .setThreadingModel(ThreadingModel.VIRTUAL_THREAD);
    vertx.deployVerticle(new MainVerticle(), deploymentOptions)
      .onComplete(ar -> {
        if (ar.succeeded()) {
          System.out.println("Done");
        } else {
          ar.cause().printStackTrace();
        }
      });
  }

  @Override
  public void start(Promise<Void> startPromise) {
    vertx.createHttpServer().requestHandler(req -> {
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello from Vert.x!");
    }).listen(8888, http -> {
      if (http.succeeded()) {
        startPromise.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startPromise.fail(http.cause());
      }
    });
  }
}

The JVM stops before the server is bound. It does not happen if the verticle is deployed using the event loop or the worker threading model.

@tsegismont tsegismont added the bug label Jan 5, 2024
@tsegismont tsegismont added this to the 4.5.2 milestone Jan 5, 2024
@tsegismont
Copy link
Contributor Author

Quick analysis: when the virtual thread model is used, no event loop is created before the http server is started. Vert.x threads are non-daemon by default, and they prevent the JVM from shutting down.

In the sample before, the application reaches the end of the main method before the virtual thread triggers the creation of the http server.

That is why the JVM shut downs early.

@tsegismont
Copy link
Contributor Author

cc @vietj

Many applications rely on the fact that the JVM will stay alive even after the main thread reaches the end of the method.

@vietj
Copy link
Member

vietj commented Jan 9, 2024

yeah I found this issue too a while ago, I don't know yet what is the best way to address it.

I believe that applications should not rely on vertx though

@tsegismont
Copy link
Contributor Author

I just found that this (funny?) behavior:

vertx.deployVerticle(new MainVerticle(), deploymentOptions)

to:

vertx.deployVerticle(MainVerticle.class.getName(), deploymentOptions)

Then the JVM doesn't stop because Vert.x starts an event loop

I believe that applications should not rely on vertx though

I understand. On the other hand, we have so many examples which look like the example in the description.

@tsegismont
Copy link
Contributor Author

Perhaps we can find the difference between deploying a verticle with a class name or with a class or an instance, and make sure it always works like deploying using a class name.

@tsegismont tsegismont self-assigned this Jan 9, 2024
@vietj
Copy link
Member

vietj commented Jan 9, 2024

I believe we should start to provide different examples when the code is started from a non vertx thread, e.g.

vertx.deployVerticle(...).await();

would make sense to me

  • it forces the main thread to await the success or failure of the deployment
  • it will allow event-loop to be started and prevent the exit

However await() currently is only supported on worker threads, but it would make sense to support it from a non vertx event-loop thread.

@vietj vietj modified the milestones: 4.5.2, 4.5.3 Jan 30, 2024
@vietj vietj modified the milestones: 4.5.3, 4.5.4 Feb 6, 2024
@vietj vietj modified the milestones: 4.5.4, 4.5.5 Feb 22, 2024
@vietj vietj modified the milestones: 4.5.5, 4.5.6 Mar 14, 2024
@tsegismont tsegismont removed their assignment Mar 14, 2024
@tsegismont tsegismont modified the milestone: 4.5.6 Mar 14, 2024
@vietj vietj modified the milestones: 4.5.6, 4.5.7, 4.5.8 Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants