Skip to content

Commit 7e85cac

Browse files
Throw clearer error message during Stash() in null message cases (#7425)
close #7938
1 parent 487bb85 commit 7e85cac

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="Bugfix7398Specs.cs" company="Akka.NET Project">
3+
// Copyright (C) 2009-2024 Lightbend Inc. <http://www.lightbend.com>
4+
// Copyright (C) 2013-2024 .NET Foundation <https://github.com/akkadotnet/akka.net>
5+
// </copyright>
6+
// -----------------------------------------------------------------------
7+
8+
using Akka.Actor;
9+
using Akka.TestKit;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
namespace Akka.Tests.Actor.Stash;
14+
15+
public class Bugfix7398Specs : AkkaSpec
16+
{
17+
public Bugfix7398Specs(ITestOutputHelper output)
18+
: base(output)
19+
{
20+
}
21+
22+
private class IllegalStashActor : UntypedActor, IWithStash
23+
{
24+
protected override void OnReceive(object message)
25+
{
26+
27+
}
28+
29+
protected override void PreStart()
30+
{
31+
// ILLEGAL
32+
Stash.Stash();
33+
}
34+
35+
public IStash Stash { get; set; }
36+
}
37+
38+
[Fact]
39+
public void Should_throw_exception_when_stashing_in_PreStart()
40+
{
41+
EventFilter.Exception<ActorInitializationException>().ExpectOne(() =>
42+
{
43+
var actor = Sys.ActorOf(Props.Create<IllegalStashActor>());
44+
actor.Tell("hello");
45+
});
46+
}
47+
}

src/core/Akka/Actor/Stash/Internal/AbstractStash.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ public void Stash()
7474
{
7575
var currMsg = _actorCell.CurrentMessage;
7676
var sender = _actorCell.Sender;
77-
77+
7878
if (_actorCell.CurrentEnvelopeId == _currentEnvelopeId)
7979
{
80+
if(currMsg is null)
81+
throw new InvalidOperationException("There is no message to stash right now. Stash() must be called inside an actor's Receive methods.");
8082
throw new IllegalActorStateException($"Can't stash the same message {currMsg} more than once");
8183
}
8284
_currentEnvelopeId = _actorCell.CurrentEnvelopeId;

0 commit comments

Comments
 (0)