在 Akka.net 出现异常后未收到终止

Not receiving Terminated after Exception in Akka.net

我有两个演员(一个 parent 和一个 child),我使用 SupervisorStrategy.StoppingStrategyWatch 来处理错误。但问题是 parent 在 child 死亡时没有收到 Terminated 消息。

下面是一个简单的测试来验证我的问题。

    [Fact]
    public void ExceptionTest()
    {
        var child = Sys.ActorOf(Props.Create(() => new FooActor(), SupervisorStrategy.StoppingStrategy), "Name");
        Watch(child);
        child.Tell("Some message");
        ExpectMsg<Terminated>(); // This will not occur.
    }

    public class FooActor : ReceiveActor
    {
        public FooActor()
        {
            ReceiveAny((e) =>
            {
                // Crashes on every message.
                throw new Exception();
            });
        }
    }

我想要的是,只要 child 有一个它无法处理的错误,正在监视 child 的 parent 应该得到一个 Terminated。我怎样才能完成它?

当我发送 PoisonPill 而不是抛出 Exception 时它工作正常。

测试中的actor似乎是用重启监督策略创建的,因此在异常发生时不会杀死actor。我找不到改变测试主管策略的方法。所以我写这个是为了在测试中创建演员停止监督策略而不必写一个"real"包装演员。

    [Fact]
    public async Task ExceptionTest()
    {
        var actor = await CreateActorWithStoppingStrategy(Props.Create(() => new AsGoodAsDead()));

        Watch(actor);

        actor.Tell("Death");

        ExpectTerminated(actor);
    }

    private async Task<IActorRef> CreateActorWithStoppingStrategy(Props props)
    {
        IActorRef child = null;
        Action<IActorDsl> actor = d =>
        {
            d.Strategy   = SupervisorStrategy.StoppingStrategy;
            d.OnPreStart = context => child = context.ActorOf(props);
            d.ReceiveAny((o, context) => context.Sender.Tell("ready"));
        };
        var parent = Sys.ActorOf(Props.Create(() => new Act(actor)));
        await parent.Ask("ready?");
        return child;
    }

    public class AsGoodAsDead : UntypedActor
    {
        protected override void OnReceive(object message)
        {
            throw new Exception();
        }
    }

你写的下面这行会给child stopping supervisor 策略,意味着child 的children 会在发生某些事情时停止。

    var child = Sys.ActorOf(Props.Create(() => new FooActor(), SupervisorStrategy.StoppingStrategy), "Name");