Are all implementations of ActorRefFactory thread safe

Hi! Is using actorOf from classic ActorRefFactory thread safe? Simplified implementation I am looking at is:

class MyActorBuilder {
  def buildFor(factory: ActorRefFactory): Future[ActorRef] = Future { factory.actorOf(props) }
}

Since ActorRefFactory is implemented by both, ActorSystem and ActorContext, I am wondering whether it is safe to call buildFor from an actor passing actors’ context as an argument.

I am planning to use it to externalize making the child from the parent in a similar fashion as described here: https://doc.akka.io/docs/akka/current/testing.html#externalize-child-making-from-the-parent

Hi @mcveat,

the docs leave it unspecified whether those are thread-safe or not so you should probably not assume thread-safety.

Why do you want to call those asynchronously in the first place? actorOf is mostly an asynchronous operation (especially actor class creation and calling of preStart etc is done asynchronously) so it seems running actorOf asynchronously would only complicates things.

Johannes

ActorContext is one of the most common ActorRefFactory instances and for that it is not safe to call actorOf from any thread but the one handling a message to the actor. There actorOf spawns children to the actor which is part of the internal mutable actor state.