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

Throw clearer error message during Stash() in null message cases #7425

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/core/Akka.Tests/Actor/Stash/Bugfix7398Specs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// -----------------------------------------------------------------------
// <copyright file="Bugfix7398Specs.cs" company="Akka.NET Project">
// Copyright (C) 2009-2024 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2024 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using Akka.Actor;
using Akka.TestKit;
using Xunit;
using Xunit.Abstractions;

namespace Akka.Tests.Actor.Stash;

public class Bugfix7398Specs : AkkaSpec
{
public Bugfix7398Specs(ITestOutputHelper output)
: base(output)
{
}

private class IllegalStashActor : UntypedActor, IWithStash
{
protected override void OnReceive(object message)
{

}

protected override void PreStart()
{
// ILLEGAL
Stash.Stash();
}

public IStash Stash { get; set; }
}

[Fact]
public void Should_throw_exception_when_stashing_in_PreStart()
{
EventFilter.Exception<ActorInitializationException>().ExpectOne(() =>
{
var actor = Sys.ActorOf(Props.Create<IllegalStashActor>());
actor.Tell("hello");
});
}
}
4 changes: 3 additions & 1 deletion src/core/Akka/Actor/Stash/Internal/AbstractStash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public void Stash()
{
var currMsg = _actorCell.CurrentMessage;
var sender = _actorCell.Sender;

if (_actorCell.CurrentEnvelopeId == _currentEnvelopeId)
{
if(currMsg is null)
throw new InvalidOperationException("There is no message to stash right now. Stash() must be called inside an actor's Receive methods.");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide a clearer explanation of why we're throwing an exception

throw new IllegalActorStateException($"Can't stash the same message {currMsg} more than once");
}
_currentEnvelopeId = _actorCell.CurrentEnvelopeId;
Expand Down
Loading