Ivan Mushketyk
Ivan Mushketyk

Reputation: 8305

AskTimeoutException: on spray-can server stop

I am trying to stop spray-can web server with the following code:

implicit val timeout = Timeout(10 seconds)
val future = ask(IO(Http)(system), Http.Unbind(10 second))
Await.result(future, Duration.Inf)

but unfortunatelly I receive the following exception:

[error] AskTimeoutException: : Timed out (AskSupport.scala:334) [error] akka.pattern.PromiseActorRef$$anonfun$1.apply$mcV$sp(AskSupport.scala:334) [error] akka.actor.Scheduler$$anon$11.run(Scheduler.scala:118) [error] akka.actor.LightArrayRevolverScheduler$TaskHolder.executeTask(Scheduler.scala:455) [error] akka.actor.LightArrayRevolverScheduler$$anon$12.executeBucket$1(Scheduler.scala:407) [error] akka.actor.LightArrayRevolverScheduler$$anon$12.nextTick(Scheduler.scala:411) [error] akka.actor.LightArrayRevolverScheduler$$anon$12.run(Scheduler.scala:363)

What am I doing wrong?

Upvotes: 3

Views: 631

Answers (1)

Christian
Christian

Reputation: 4593

The problem is you are sending the Http.Unbind message to the wrong actor (i.e. the manager actor for the IO extension - in this case, Http).

You have to send the Http.Unbind message to the HttpListener (this is the actor that replies to the Http.Bind message with an Http.Bound message). The following example sends Http.Bind to the manager actor and Http.Unbind to the HttpListener:

class TestActor extends Actor {
  override def preStart = {
    IO(Http) ! Http.Bind(self, interface = "localhost", port = 8080)
  }
  def receive = {
    case Http.Bound(_) => 
      println("bound")
      sender ! Http.Unbind(10 seconds)
    case Http.Unbound =>
      println("unbound")
      context.stop(self)
  }
}

More information can be found in the documentation section on starting and stopping.

Upvotes: 3

Related Questions