Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for AK1002 #7066

Merged
merged 4 commits into from Jan 18, 2024

Conversation

Arkatufus
Copy link
Contributor


uid: AK1002
title: Akka.Analyzers Rule AK1002 - "Must not await Self.GracefulStop() inside ReceiveAsync or ReceiveAnyAsync"

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();
      }):
   }
}

@Aaronontheweb
Copy link
Member

@Arkatufus might also be worth mentioning that this can affect an UntypedActor.RunTask with the same type of await on a GracefulStop but that's even more of an edge case I suspect.

@Aaronontheweb Aaronontheweb merged commit 809c432 into akkadotnet:dev Jan 18, 2024
12 checks passed
@Aaronontheweb Aaronontheweb added docs rosyln-analyzer Issues that should be addressed via user-exposed Roslyn analyzers. labels Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs rosyln-analyzer Issues that should be addressed via user-exposed Roslyn analyzers.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants