akkadotnet/akka.net

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

Open

#3691 aperta il 28 dic 2018

Vedi su GitHub
 (13 commenti) (3 reazioni) (0 assegnatari)C# (1035 fork)batch import
akka-actorhelp wantedpotential bug

Metriche repository

Star
 (4543 star)
Metriche merge PR
 (Merge medio 2g 21h) (32 PR mergiate in 30 g)

Descrizione

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;
    }
}

Guida contributor