From 07e9ad82b2e42d5e728da075bbf9bb58c9b1a3ff Mon Sep 17 00:00:00 2001 From: Ismael Hamed <1279846+ismaelhamed@users.noreply.github.com> Date: Wed, 20 Nov 2019 17:40:25 +0100 Subject: [PATCH] Stop DeadLetterListener on terminate if LogDeadLettersDuringShutdown is disabled (#4042) --- .../CoreAPISpec.ApproveCore.approved.txt | 1 + src/core/Akka.Tests/Actor/ActorSystemSpec.cs | 20 ++++++++++++++++ .../Akka/Actor/Internal/ActorSystemImpl.cs | 2 ++ src/core/Akka/Event/DeadLetterListener.cs | 23 +++++++++++-------- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt index 345fc6af3aa..b66f857bff9 100644 --- a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt +++ b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt @@ -2878,6 +2878,7 @@ namespace Akka.Event public DeadLetterListener() { } protected override void PostRestart(System.Exception reason) { } protected override void PostStop() { } + protected override void PreRestart(System.Exception reason, object message) { } protected override void PreStart() { } protected override bool Receive(object message) { } } diff --git a/src/core/Akka.Tests/Actor/ActorSystemSpec.cs b/src/core/Akka.Tests/Actor/ActorSystemSpec.cs index 3314fd5ad06..8e6c9ae49bd 100644 --- a/src/core/Akka.Tests/Actor/ActorSystemSpec.cs +++ b/src/core/Akka.Tests/Actor/ActorSystemSpec.cs @@ -105,6 +105,26 @@ public void Allow_valid_names() .Terminate(); } + [Fact] + public void Log_dead_letters() + { + var sys = ActorSystem.Create("LogDeadLetters", ConfigurationFactory.ParseString("akka.loglevel=INFO") + .WithFallback(DefaultConfig)); + + try + { + var a = sys.ActorOf(Props.Create()); + + var eventFilter = new EventFilterFactory(new TestKit.Xunit2.TestKit(sys)); + eventFilter.Info(contains: "not delivered").Expect(1, () => + { + a.Tell("run"); + a.Tell("boom"); + }); + } + finally { Shutdown(sys); } + } + [Fact] public void Block_until_exit() { diff --git a/src/core/Akka/Actor/Internal/ActorSystemImpl.cs b/src/core/Akka/Actor/Internal/ActorSystemImpl.cs index b126fcaef54..3b77770d662 100644 --- a/src/core/Akka/Actor/Internal/ActorSystemImpl.cs +++ b/src/core/Akka/Actor/Internal/ActorSystemImpl.cs @@ -493,6 +493,8 @@ public override void RegisterOnTermination(Action code) public override Task Terminate() { Log.Debug("System shutdown initiated"); + if (!Settings.LogDeadLettersDuringShutdown && _logDeadLetterListener != null) + Stop(_logDeadLetterListener); _provider.Guardian.Stop(); return WhenTerminated; } diff --git a/src/core/Akka/Event/DeadLetterListener.cs b/src/core/Akka/Event/DeadLetterListener.cs index d4cdf905026..1385ca55de4 100644 --- a/src/core/Akka/Event/DeadLetterListener.cs +++ b/src/core/Akka/Event/DeadLetterListener.cs @@ -20,13 +20,19 @@ public class DeadLetterListener : ActorBase private int _count; /// - /// TBD + /// Don't re-subscribe, skip call to preStart /// - /// TBD protected override void PostRestart(Exception reason) { } + /// + /// Don't remove subscription, skip call to postStop, no children to stop + /// + protected override void PreRestart(Exception reason, object message) + { + } + /// /// TBD /// @@ -55,7 +61,7 @@ protected override bool Receive(object message) var rcp = deadLetter.Recipient; _count++; - + var done = _maxCount != int.MaxValue && _count >= _maxCount; var doneMsg = done ? ", no more dead letters will be logged" : ""; @@ -65,15 +71,12 @@ protected override bool Receive(object message) var sndPath = snd == ActorRefs.NoSender ? "NoSender" : snd.Path.ToString(); _eventStream.Publish(new Info(rcpPath, rcp.GetType(), - string.Format("Message {0} from {1} to {2} was not delivered. {3} dead letters encountered.{4}", - deadLetter.Message.GetType().Name, sndPath, rcpPath, _count, doneMsg))); - } - - if (done) - { - ((IInternalActorRef) Self).Stop(); + $"Message [{deadLetter.Message.GetType().Name}] from {sndPath} to {rcpPath} was not delivered. [{_count}] dead letters encountered {doneMsg}." + + "This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' " + + "and 'akka.log-dead-letters-during-shutdown'.")); } + if (done) Context.Stop(Self); return true; } }