Home > other >  Scala App doesn't exit even though Future is completed
Scala App doesn't exit even though Future is completed

Time:10-27

I wait for a Future to complete and print the content on the console. Even when everything is finished, the main application doesn't exit and I have to kill it manually.

def main(args: Array[String]): Unit {
    val req = HttpRequest(GET, myURL)
    val res = Http().singleRequest(req)
    val resultsFutures = Future {
        val resultString = Await.result(HttpRequests.unpackResponse(res), Duration.Inf)
        JsonMethods.parse(resultString).extract[List[Results]]
    }
    val results = Await.result(resultsFutures, Duration.Inf)
    println(results)
}

So results gets printed on the console with the expected contend, but the application still doesn't end. Is there something I can do to exit the application? Is there still something running, that the main is waiting for?

I'm using:

  • scala 2.12.10
  • akka 2.5.26
  • akkaHttp 10.1.11

CodePudding user response:

As you are using Akka, you likely have an ActorSystem instantiated somehow under the hood that will keep the process running.

Either you are able to get a hand on it and call its actorSystem.terminate() method, or you can also use an explicit sys.exit(0) at the end of your main method (0 being the exit code you want).

Edit: you should also wrap the Awaits in Try and make sure to call sys.exit in case of failures as well.

  • Related