Skip to content

Commit

Permalink
distinguish between fatal and normal fault
Browse files Browse the repository at this point in the history
  • Loading branch information
Roiocam committed Jul 3, 2024
1 parent 5d415bd commit e8a5787
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import scala.concurrent.duration._

import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory

import language.postfixOps
import org.scalatest.BeforeAndAfterEach

Expand Down Expand Up @@ -543,25 +544,25 @@ class SupervisorSpec

"supervise exceptions on child actor initialize" in {
val parent = system.actorOf(Props(new Actor {
val cnt: AtomicInteger = new AtomicInteger(0)
var childRef: ActorRef = _
val cnt: AtomicInteger = new AtomicInteger(0)
var childRef: ActorRef = _

override def supervisorStrategy: SupervisorStrategy = OneForOneStrategy() {
case _: ActorInitializationException => SupervisorStrategy.Restart
case _ => SupervisorStrategy.Stop
}
override def supervisorStrategy: SupervisorStrategy = OneForOneStrategy() {
case _: ActorInitializationException => SupervisorStrategy.Restart
case _ => SupervisorStrategy.Stop
}

override def preStart(): Unit = {
childRef = context.actorOf(Props(new Child(cnt.getAndIncrement())))
}
override def preStart(): Unit = {
childRef = context.actorOf(Props(new Child(cnt.getAndIncrement())), "child")
}

def childAlive(): Boolean = childRef != null && !childRef.isTerminated
def childAlive(): Boolean = childRef != null && !childRef.isTerminated

def receive = {
case msg if msg == PingMessage && childAlive() =>
sender() ! PongMessage
}
}))
def receive = {
case msg if msg == PingMessage && childAlive() =>
sender() ! PongMessage
}
}), "parent")

val probe = TestProbe()
probe.send(parent, PingMessage)
Expand Down
4 changes: 3 additions & 1 deletion actor/src/main/scala/org/apache/pekko/actor/ActorCell.scala
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,6 @@ private[pekko] class ActorCell(
def failActor(): Unit =
if (_actor != null) {
clearActorFields(actor, recreate = false)
setFailed(actor.self)
_actor = null // ensure that we know that we failed during creation
}

Expand All @@ -658,6 +657,7 @@ private[pekko] class ActorCell(
} catch {
case e: InterruptedException =>
failActor()
setFailedFatally()
Thread.currentThread().interrupt()
throw ActorInitializationException(self, "interruption during creation", e)
case NonFatal(e) =>
Expand Down Expand Up @@ -717,6 +717,8 @@ private[pekko] class ActorCell(
final protected def clearActorFields(actorInstance: Actor, recreate: Boolean): Unit = {
currentMessage = null
behaviorStack = emptyBehaviorStack
if (recreate) setFailed(actorInstance.self)
else setFailed(system.deadLetters)
}
final protected def clearFieldsForTermination(): Unit = {
unstashAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ private[pekko] trait FaultHandling { this: ActorCell =>
publish(Error(e, self.path.toString, clazz(freshActor), "restarting " + child))
})
} catch handleNonFatalOrInterruptedException { e =>
setFailedFatally()
clearActorFields(actor, recreate = false) // in order to prevent preRestart() from happening again
setFailedFatally()
handleInvokeFailure(survivors, PostRestartException(self, e, cause))
}
}
Expand Down

0 comments on commit e8a5787

Please sign in to comment.