perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

Running port() returning 0 if called to quickly #1253

Open weeryan17 opened 2 years ago

weeryan17 commented 2 years ago

When trying to get the port spark is actually listening on with the port being set to 0, it can return 0 if called too quickly. This seams to be because the server is initializing in a separate thread and therefor not setting the port number to the actual port number before calling port(). See example

public int initWeb() {
    port(0);
    get("/thing", (req, res) -> ...);
    return port(); // returns 0
}

There also seams to be no way to check for the server being fully initialized as far as I can tell, but I could be wrong about that.

corbs-blis commented 2 years ago

Try: Spark.awaitInitialization();