From 7e85cac33490db998db36774e47ef571f444a49e Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Thu, 19 Dec 2024 15:34:46 -0600 Subject: [PATCH] Throw clearer error message during `Stash()` in `null` message cases (#7425) close #7938 --- .../Akka.Tests/Actor/Stash/Bugfix7398Specs.cs | 47 +++++++++++++++++++ .../Actor/Stash/Internal/AbstractStash.cs | 4 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/core/Akka.Tests/Actor/Stash/Bugfix7398Specs.cs diff --git a/src/core/Akka.Tests/Actor/Stash/Bugfix7398Specs.cs b/src/core/Akka.Tests/Actor/Stash/Bugfix7398Specs.cs new file mode 100644 index 00000000000..b0c8a8f9d15 --- /dev/null +++ b/src/core/Akka.Tests/Actor/Stash/Bugfix7398Specs.cs @@ -0,0 +1,47 @@ +// ----------------------------------------------------------------------- +// +// Copyright (C) 2009-2024 Lightbend Inc. +// Copyright (C) 2013-2024 .NET Foundation +// +// ----------------------------------------------------------------------- + +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().ExpectOne(() => + { + var actor = Sys.ActorOf(Props.Create()); + actor.Tell("hello"); + }); + } +} \ No newline at end of file diff --git a/src/core/Akka/Actor/Stash/Internal/AbstractStash.cs b/src/core/Akka/Actor/Stash/Internal/AbstractStash.cs index 0e925deefd6..fe04ef85d47 100644 --- a/src/core/Akka/Actor/Stash/Internal/AbstractStash.cs +++ b/src/core/Akka/Actor/Stash/Internal/AbstractStash.cs @@ -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."); throw new IllegalActorStateException($"Can't stash the same message {currMsg} more than once"); } _currentEnvelopeId = _actorCell.CurrentEnvelopeId;