diff --git a/src/core/Akka/Actor/ActorCell.cs b/src/core/Akka/Actor/ActorCell.cs index cab41a7ed7d..99e260aa1a3 100644 --- a/src/core/Akka/Actor/ActorCell.cs +++ b/src/core/Akka/Actor/ActorCell.cs @@ -533,17 +533,23 @@ private Envelope SerializeAndDeserialize(Envelope envelope) object deserializedMsg; try { - deserializedMsg = SerializeAndDeserializePayload(envelope.Message); + deserializedMsg = SerializeAndDeserializePayload(unwrapped); } catch (Exception e) { throw new SerializationException($"Failed to serialize and deserialize payload object [{unwrapped.GetType()}]. Envelope: [{envelope}], Actor type: [{Actor.GetType()}]", e); } - // special case handling for DeadLetters - return envelope.Message is DeadLetter deadLetter - ? new Envelope(new DeadLetter(deserializedMsg, deadLetter.Sender, deadLetter.Recipient), envelope.Sender) - : new Envelope(deserializedMsg, envelope.Sender); + // Check that this message was ever wrapped + if (ReferenceEquals(envelope.Message, unwrapped)) + return new Envelope(deserializedMsg, envelope.Sender); + + // In the case above this one, we're able to deserialize the message, and we returned that + // (a new copy of the message), however, when we're dealing with wrapped messages, it's impossible to + // reconstruct the russian dolls together again, so we just return the original envelope. + // We guarantee that we can de/serialize in innermost message, but we can not guarantee to return + // a new copy. + return envelope; } #nullable restore diff --git a/src/core/Akka/Event/DeadLetter.cs b/src/core/Akka/Event/DeadLetter.cs index 62354198208..414bcbe3ca5 100644 --- a/src/core/Akka/Event/DeadLetter.cs +++ b/src/core/Akka/Event/DeadLetter.cs @@ -83,6 +83,7 @@ public sealed class DeadLetter : AllDeadLetters /// public DeadLetter(object message, IActorRef sender, IActorRef recipient) : base(message, sender, recipient) { + System.Diagnostics.Debug.Assert(message is not DeadLetter, "DeadLetter inside DeadLetter"); if (sender == null) throw new ArgumentNullException(nameof(sender), "DeadLetter sender may not be null"); if (recipient == null) throw new ArgumentNullException(nameof(recipient), "DeadLetter recipient may not be null"); }