From ad50778622215bdc86dd13fb0ea9697d450655ba Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Thu, 27 Feb 2020 18:24:34 -0600 Subject: [PATCH] unregister AppDomain.ProcessExit event handler after ActorSystem terminates (#3846) * close #3735 - unregister AppDomain.ProcessExit event handler after ActorSystem terminates * fixed typo on save --- src/core/Akka/Actor/CoordinatedShutdown.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/core/Akka/Actor/CoordinatedShutdown.cs b/src/core/Akka/Actor/CoordinatedShutdown.cs index 920974ef62b..11160c50258 100644 --- a/src/core/Akka/Actor/CoordinatedShutdown.cs +++ b/src/core/Akka/Actor/CoordinatedShutdown.cs @@ -673,12 +673,10 @@ internal static void InitClrHook(ActorSystem system, Config conf, CoordinatedShu var runByClrShutdownHook = conf.GetBoolean("run-by-clr-shutdown-hook", false); if (runByClrShutdownHook) { + var exitTask = TerminateOnClrExit(coord); // run all hooks during termination sequence - AppDomain.CurrentDomain.ProcessExit += (sender, args) => - { - // have to block, because if this method exits the process exits. - coord.RunClrHooks().Wait(coord.TotalTimeout); - }; + AppDomain.CurrentDomain.ProcessExit += exitTask; + system.WhenTerminated.ContinueWith(tr => { AppDomain.CurrentDomain.ProcessExit -= exitTask; }); coord.AddClrShutdownHook(() => { @@ -702,5 +700,14 @@ internal static void InitClrHook(ActorSystem system, Config conf, CoordinatedShu }); } } + + private static EventHandler TerminateOnClrExit(CoordinatedShutdown coord) + { + return (sender, args) => + { + // have to block, because if this method exits the process exits. + coord.RunClrHooks().Wait(coord.TotalTimeout); + }; + } } }