Q:

SparkJava awaitStop() method returns immediately, even after initialization, but stop() has not been called

I have a small REST service built with SparkJava 2.9.1 that includes an API for changing the bound IP address and port of the service. If those settings are changed I want to stop the current service and restart it with the new values, without exiting the JVM.
My basic process for doing this is:

Start the service.
Call awaitInitialiation() (because I don't want to call awaitStop() until its initialized).
Call awaitStop() and wait for the current service to complete.
Start with the new parameters.

I'm using the instance interface to do this as I have two services, one on HTTP and the other on HTTPS.
A stripped down version of this, without the actual restart, looks like this:
public class SparkTest {
    public static void main(String[] args) {
        Service spark = ignite()
                .ipAddress("127.0.0.1")
                .port(3141);

        // Should be a post(), using get() so I can test from a browser.
        spark.get("/Stop", (req, res) -> { spark.stop(); return true; });

        spark.awaitInitialization();
        System.out.println("Initialized at: " + Instant.now().toString());

        spark.awaitStop();
        System.out.println("Stopped at: " + Instant.now().toString());

       // I want to start a new service here with a new address and/or port.
    }
}

I expect the "Initialized at" message to be printed out immediately, but the "Stopped at" message should not print until I try a "http://127.0.0.1:3141/Stop" from a browser.
However what happens is both messages print out almost immediately in sequence.
Initialized at: 2020-02-28T17:20:31.291Z
Stopped at: 2020-02-28T17:20:31.291Z

Fundamentally I want a way to stop the currently running service, make sure it's completely finished running, then start up a new service, all in the currently running JVM.

A:

Short answer: awaitStop() uses await() on a countdown latch, but that latch is not initialized until stop is called.
awaitStop() in Service.java
    public void awaitStop() {
        try {
            stopLatch.await();

Inside stop():
   public synchronized void stop() {
        // Some code elided.
        initiateStop();

Inside initateStop():
    private void initiateStop() {
        stopLatch = new CountDownLatch(1);

So if want to wait for a specific restart signal I can add my own latch:
        CountDownLatch latch = new CountDownLatch(1);

        spark.get("/Stop", (req, res) -> { latch.countDown(); spark.stop(); return true; });

        spark.awaitInitialization();
        System.out.println("Initialized at: " + Instant.now().toString());

        latch.await();
        spark.awaitStop();

And this seems to give the result I wanted.

