akkadotnet/akka.net

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

Open

#3.691 geöffnet am 28. Dez. 2018

Auf GitHub ansehen
 (13 Kommentare) (3 Reaktionen) (0 zugewiesene Personen)C# (1.035 Forks)batch import
akka-actorhelp wantedpotential bug

Repository-Metriken

Stars
 (4.543 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 2T 21h) (32 gemergte PRs in 30 T)

Beschreibung

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

Contributor Guide