akkadotnet/akka.net

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

Open

#3.691 aberto em 28 de dez. de 2018

Ver no GitHub
 (13 comments) (3 reactions) (0 assignees)C# (1.035 forks)batch import
akka-actorhelp wantedpotential bug

Métricas do repositório

Stars
 (4.543 stars)
Métricas de merge de PR
 (Mesclagem média 2d 21h) (32 fundiu PRs em 30d)

Description

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

Guia do colaborador