How to "kill" an actor?

346 views
Skip to first unread message

Michi

unread,
Dec 10, 2013, 4:20:04 AM12/10/13
to akka...@googlegroups.com
Hi,

how can I "kill" an actor that hangs? Here is an example:

import akka.actor.{Props, Actor, ActorSystem}
import akka.pattern.ask
import scala.util.{Failure, Success}
import akka.util.Timeout

case object Ping
case object Pong


object ShutdownTest {
  def main(args: Array[String]) {
    val system = ActorSystem("shutdown-test")
    val a = system.actorOf(Props[TestActor])
    implicit val timeout = Timeout(2000)
    import scala.concurrent.ExecutionContext.Implicits.global
    (a ? Ping).mapTo[Pong.type] onComplete {
      case Success(pong) => println("received Pong")
      case Failure(reason) =>
        println("Ping timed out: " + reason)
        system.stop(a)
    }
    Thread.sleep(3000)
    system.shutdown()
  }
}

class TestActor extends Actor {
  Thread.sleep(Int.MaxValue)

  def receive = {
    case Ping => sender ! Pong
  }

  override def postStop() {
    println("Killed")
  }
}

This just hangs forever. system.stop does not stop the actor and system.shutdown does not work either.

This example is obviously quite artifical. But how can you actually stop actors that block on an IO operation (because there is some error etc.) etc.? One solution would obviously be to use threads and just interrupt / kill them if they hang but this does not seem very nice to me.

Thanks,
Michael

√iktor Ҡlang

unread,
Dec 10, 2013, 4:26:11 AM12/10/13
to Akka User List
There is no way on the JVM to do that safely.


--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.



--
Cheers,

Viktor Klang

Director of Engineering

Twitter: @viktorklang

Michael Thaler

unread,
Dec 10, 2013, 4:31:58 AM12/10/13
to akka...@googlegroups.com
Hi Viktor,
 
There is no way on the JVM to do that safely.

In the above example Thread.interrupt would cause an interrupted exception which I think is safe. I guess interrupting a threadpool-thread is not a good idea (if the threadpool does not recognize it and create a new one) or is there another reason that there is no context.interrupt(ref) method?

I know that killing threads is not safe, but sometimes there is just no other way. Is there any way to "kill" an actor, even if it is not safe?
 

Rüdiger Klaehn

unread,
Dec 10, 2013, 4:48:45 AM12/10/13
to akka...@googlegroups.com
This has been discussed before. I guess the bottom line is that you should not do it. If you want to do it you have to make sure that the actor runs with its own specialized executor, call thread.stop when something hangs and pray to a deity of your choice.

https://groups.google.com/forum/#!searchin/akka-user/kill$20long-running$20actor/akka-user/TlIbfaC1eb8/EQ8S2NoI-oIJ


Roland Kuhn

unread,
Dec 10, 2013, 5:07:55 AM12/10/13
to akka-user
Except that Thread.stop() has finally been actually removed (well, it throws UnsupportedOperationException) in Java 8, which removes the need to choose a deity ;-)

Michael, implementing context.interrupt(ref) would have quite some runtime overhead due to the book-keeping and safe-guards which we would have to put in place, and the problem I have with that is that everyone pays for it (also by having one more delicate code path within Akka) while only very few will ever use it.

The obvious work-around is to not use blocking IO, which is quite established for network operations and also possible for file operations.

If you must use some other library which does blocking calls, then you can also spawn a dedicated thread for running that and have an actor manage it; in this case you can call Thread.interrupt or Thread.stop any way you like while keeping the actor responsive.

Regards,

Roland


Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


√iktor Ҡlang

unread,
Dec 10, 2013, 5:42:22 AM12/10/13
to Akka User List
On Tue, Dec 10, 2013 at 11:31 AM, Michael Thaler <michael...@googlemail.com> wrote:
Hi Viktor,
 
There is no way on the JVM to do that safely.

In the above example Thread.interrupt would cause an interrupted exception which I think is safe.

Thread.interrupt only works for code that regularly calls Thread.isInterrupted so it's not a solution.

Cheers,
 
I guess interrupting a threadpool-thread is not a good idea (if the threadpool does not recognize it and create a new one) or is there another reason that there is no context.interrupt(ref) method?

I know that killing threads is not safe, but sometimes there is just no other way. Is there any way to "kill" an actor, even if it is not safe?
 

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages