akkadotnet/akka.net

Disposable object created in AroundReceive gets disposed during the middle of ReceiveAsync

Open

#3,691 opened on 2018年12月28日

GitHub で見る
 (13 comments) (3 reactions) (0 assignees)C# (4,543 stars) (1,035 forks)batch import
akka-actorhelp wantedpotential bug

説明

I'm not sure this hasn't been reported before, as I see it is referenced in the following article:

https://havret.io/akka-entity-framework-core

At least until you start playing with AroundReceive, which isn’t an async method and simply returns before your handler does its async work.

I find AroundReceive to be incredibly useful, and being able to use it with Actors that make async calls seems pretty important.

Quick repro in case the problem isn't clear from the article:

class Program
{
    static void Main(string[] args)
    {
        var actorSystem = ActorSystem.Create("tempActorSystem");
        var actorRef = actorSystem.ActorOf(Props.Create<MyActor>(), "myActor");
        actorRef.Tell("hello");

        Console.ReadLine();
    }
}

public class MyActor : ReceiveActor
{
    public MyActor()
    {
        ReceiveAsync<string>(async message => await DoAsyncWork(message));
    }

    private async Task DoAsyncWork(string message)
    {
        await Task.Delay(2000);
        Console.WriteLine($"Finished DoAsynchWork at {DateTime.Now}");
    }

    protected override bool AroundReceive(Receive receive, object message)
    {
        base.AroundReceive(receive, message);
        Console.WriteLine($"Finished AroundReceive at {DateTime.Now}");
        return true;
    }
}

コントリビューターガイド