Search Results for

    Show / Hide Table of Contents

    AK1002 - Error

    You should never await Self.GracefulStop() inside ReceiveActor ReceiveAsync<T>() or ReceiveAnyAsync()

    Cause

    Awaiting Self.GracefulStop() inside ReceiveAsync<T>() or ReceiveAnyAsync() will cause a deadlock because the ReceiveActor will block and wait inside the message handler for itself to terminate while its PoisonPill signal is stuck inside its MailBox, waiting to be processed.

    An example:

    using Akka.Actor;
    using System.Threading.Tasks;
    using System;
    
    public sealed class MyActor : ReceiveActor
    {
       public MyActor()
       {
          ReceiveAsync<string>(async str => {
             await Context.Self.GracefulStop(); // THIS WILL DEADLOCK
          }):
       }
    }
    

    Resolution

    If you absolutely need to invoke Self.GracefulStop() inside ReceiveAsync<T>() or ReceiveAnyAsync(), make sure that you're using a detached Task instead of awaiting for it to complete.

    Here's an example below:

    using Akka.Actor;
    using System.Threading.Tasks;
    using System;
    
    public sealed class MyActor : ReceiveActor
    {
       public MyActor()
       {
          ReceiveAsync<string>(async str => {
             _ = Context.Self.GracefulStop();
          }):
       }
    }
    
    In this article
    • githubEdit this page
    Back to top
    Contribute
    • Project Chat
    • Discussion Forum
    • Source Code
    Support
    • Akka.NET Support Plans
    • Akka.NET Observability Tools
    • Akka.NET Training & Consulting
    Maintained By
    • Petabridge - The Akka.NET Company
    • Learn Akka.NET