From f9055e6c73b775fb5721d4a8ea7c433f730c05e2 Mon Sep 17 00:00:00 2001 From: "m.tre" Date: Mon, 10 Jun 2024 16:20:07 +0500 Subject: [PATCH 1/6] use posix signal registration to stop app on .net6 and greater --- Vostok.Hosting/VostokHost_Extensions.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Vostok.Hosting/VostokHost_Extensions.cs b/Vostok.Hosting/VostokHost_Extensions.cs index a4b3091b..62a7df00 100644 --- a/Vostok.Hosting/VostokHost_Extensions.cs +++ b/Vostok.Hosting/VostokHost_Extensions.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; using JetBrains.Annotations; using Vostok.Hosting.Models; @@ -53,8 +54,15 @@ public static VostokHost WithConsoleCancellation([NotNull] this VostokHost vosto /// public static VostokHost WithSigtermCancellation([NotNull] this VostokHost vostokHost) { +#if NET6_0_OR_GREATER + PosixSignalRegistration.Create(PosixSignal.SIGTERM, ctx => + { + ctx.Cancel = true; + vostokHost.Stop(false); + }); +#else AppDomain.CurrentDomain.ProcessExit += (_, _) => vostokHost.Stop(false); - +#endif return vostokHost; } } From 612b778c48bc51395d202d82851813b65916ab8a Mon Sep 17 00:00:00 2001 From: "m.tre" Date: Tue, 11 Jun 2024 12:27:10 +0500 Subject: [PATCH 2/6] fix: add comment for net 6 and greater --- Vostok.Hosting/VostokHost_Extensions.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Vostok.Hosting/VostokHost_Extensions.cs b/Vostok.Hosting/VostokHost_Extensions.cs index 62a7df00..a722a063 100644 --- a/Vostok.Hosting/VostokHost_Extensions.cs +++ b/Vostok.Hosting/VostokHost_Extensions.cs @@ -49,9 +49,15 @@ public static VostokHost WithConsoleCancellation([NotNull] this VostokHost vosto return vostokHost; } +#if NET6_0_OR_GREATER + /// + /// Listen and shutdown VostokHost if received. + /// +#else /// /// Listen and shutdown VostokHost if SIGTERM received. /// +#endif public static VostokHost WithSigtermCancellation([NotNull] this VostokHost vostokHost) { #if NET6_0_OR_GREATER From 54e1e21df3fa077e547f20498362aa1340ee4969 Mon Sep 17 00:00:00 2001 From: "m.tre" Date: Mon, 17 Jun 2024 17:32:44 +0500 Subject: [PATCH 3/6] fix: move PosixSignalRegistration to vostok host to save ref --- .../PublicAPI/net6.0/PublicAPI.Shipped.txt | 1 + .../netstandard2.0/PublicAPI.Shipped.txt | 3 +- Vostok.Hosting/VostokHost.cs | 29 +++++++++++++++++++ Vostok.Hosting/VostokHost_Extensions.cs | 26 ++--------------- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/Vostok.Hosting/PublicAPI/net6.0/PublicAPI.Shipped.txt b/Vostok.Hosting/PublicAPI/net6.0/PublicAPI.Shipped.txt index 7e061a31..934ed2c8 100644 --- a/Vostok.Hosting/PublicAPI/net6.0/PublicAPI.Shipped.txt +++ b/Vostok.Hosting/PublicAPI/net6.0/PublicAPI.Shipped.txt @@ -596,6 +596,7 @@ Vostok.Hosting.VostokHost Vostok.Hosting.VostokHost.StartAsync(Vostok.Hosting.Models.VostokApplicationState? stateToAwait = 5) -> System.Threading.Tasks.Task Vostok.Hosting.VostokHost.StopAsync(bool ensureSuccess = true) -> System.Threading.Tasks.Task Vostok.Hosting.VostokHost.VostokHost(Vostok.Hosting.VostokHostSettings settings) -> void +Vostok.Hosting.VostokHost.RegisterSigtermCancellation() -> Vostok.Hosting.VostokHost Vostok.Hosting.VostokHost_Extensions Vostok.Hosting.VostokHostingEnvironmentFactory Vostok.Hosting.VostokHostingEnvironmentFactorySettings diff --git a/Vostok.Hosting/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt b/Vostok.Hosting/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt index c3ad1bbf..128ce3ec 100644 --- a/Vostok.Hosting/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt +++ b/Vostok.Hosting/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt @@ -593,6 +593,7 @@ Vostok.Hosting.VostokHost Vostok.Hosting.VostokHost.StartAsync(Vostok.Hosting.Models.VostokApplicationState? stateToAwait = 5) -> System.Threading.Tasks.Task Vostok.Hosting.VostokHost.StopAsync(bool ensureSuccess = true) -> System.Threading.Tasks.Task Vostok.Hosting.VostokHost.VostokHost(Vostok.Hosting.VostokHostSettings settings) -> void +Vostok.Hosting.VostokHost.RegisterSigtermCancellation() -> Vostok.Hosting.VostokHost Vostok.Hosting.VostokHost_Extensions Vostok.Hosting.VostokHostingEnvironmentFactory Vostok.Hosting.VostokHostingEnvironmentFactorySettings @@ -665,4 +666,4 @@ Vostok.Hosting.VostokHostShutdown Vostok.Hosting.VostokHostShutdown.Initiate() -> void Vostok.Hosting.VostokHostShutdown.IsInitiated.get -> bool Vostok.Hosting.VostokHostShutdown.Source.get -> System.Threading.CancellationTokenSource -Vostok.Hosting.VostokHostShutdown.VostokHostShutdown(System.Threading.CancellationTokenSource source) -> void +Vostok.Hosting.VostokHostShutdown.VostokHostShutdown(System.Threading.CancellationTokenSource source) -> void \ No newline at end of file diff --git a/Vostok.Hosting/VostokHost.cs b/Vostok.Hosting/VostokHost.cs index fd1b9f21..4b1529ba 100644 --- a/Vostok.Hosting/VostokHost.cs +++ b/Vostok.Hosting/VostokHost.cs @@ -1,5 +1,8 @@ using System; using System.Diagnostics; +#if NET6_0_OR_GREATER +using System.Runtime.InteropServices; +#endif using System.Threading; using System.Threading.Tasks; using JetBrains.Annotations; @@ -51,6 +54,9 @@ public class VostokHost private volatile Task workerTask; private volatile VostokHostingEnvironment environment; private volatile ILog log; +#if NET6_0_OR_GREATER + private volatile PosixSignalRegistration sigtermRegistration; +#endif public VostokHost([NotNull] VostokHostSettings settings) { @@ -158,6 +164,29 @@ public Task StopAsync(bool ensureSuccess = true) return resultTask; } + +#if NET6_0_OR_GREATER + /// + /// Listen and shutdown VostokHost if received. + /// +#else + /// + /// Listen and shutdown VostokHost if SIGTERM received. + /// +#endif + public VostokHost RegisterSigtermCancellation() + { +#if NET6_0_OR_GREATER + sigtermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, ctx => + { + ctx.Cancel = true; + this.Stop(false); + }); +#else + AppDomain.CurrentDomain.ProcessExit += (_, _) => this.Stop(false); +#endif + return this; + } private async Task RunInternalAsync() { diff --git a/Vostok.Hosting/VostokHost_Extensions.cs b/Vostok.Hosting/VostokHost_Extensions.cs index a722a063..07718923 100644 --- a/Vostok.Hosting/VostokHost_Extensions.cs +++ b/Vostok.Hosting/VostokHost_Extensions.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using JetBrains.Annotations; using Vostok.Hosting.Models; @@ -49,27 +48,8 @@ public static VostokHost WithConsoleCancellation([NotNull] this VostokHost vosto return vostokHost; } -#if NET6_0_OR_GREATER - /// - /// Listen and shutdown VostokHost if received. - /// -#else - /// - /// Listen and shutdown VostokHost if SIGTERM received. - /// -#endif - public static VostokHost WithSigtermCancellation([NotNull] this VostokHost vostokHost) - { -#if NET6_0_OR_GREATER - PosixSignalRegistration.Create(PosixSignal.SIGTERM, ctx => - { - ctx.Cancel = true; - vostokHost.Stop(false); - }); -#else - AppDomain.CurrentDomain.ProcessExit += (_, _) => vostokHost.Stop(false); -#endif - return vostokHost; - } + [Obsolete("Use VostokHost.RegisterSigtermCancellation instead of this")] + public static VostokHost WithSigtermCancellation([NotNull] this VostokHost vostokHost) => + vostokHost.RegisterSigtermCancellation(); } } \ No newline at end of file From 1eb0c83ca68acdfbfcb1cf1de281e2103ef87dde Mon Sep 17 00:00:00 2001 From: "m.tre" Date: Tue, 18 Jun 2024 11:20:46 +0500 Subject: [PATCH 4/6] ref: add comment why save registration in variable --- Vostok.Hosting/VostokHost.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Vostok.Hosting/VostokHost.cs b/Vostok.Hosting/VostokHost.cs index 4b1529ba..2f1970a1 100644 --- a/Vostok.Hosting/VostokHost.cs +++ b/Vostok.Hosting/VostokHost.cs @@ -55,7 +55,7 @@ public class VostokHost private volatile VostokHostingEnvironment environment; private volatile ILog log; #if NET6_0_OR_GREATER - private volatile PosixSignalRegistration sigtermRegistration; + private volatile PosixSignalRegistration? sigtermRegistration; #endif public VostokHost([NotNull] VostokHostSettings settings) @@ -177,6 +177,8 @@ public Task StopAsync(bool ensureSuccess = true) public VostokHost RegisterSigtermCancellation() { #if NET6_0_OR_GREATER + // Saving PosixSignalRegistration to variable, because of IDisposable inheritance, + // and GC will collect if not assigned. On Dispose and finalize handler will unregister. sigtermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, ctx => { ctx.Cancel = true; @@ -199,6 +201,9 @@ private async Task RunInternalAsync() var dynamicThreadPool = ConfigureDynamicThreadPool(); +#if NET6_0_OR_GREATER + using (sigtermRegistration) +#endif using (environment) using (new ApplicationDisposable(settings.Application, environment, log)) using (dynamicThreadPool) From 078c74c29329572efcbc5fb1bccd5fe00e7c7489 Mon Sep 17 00:00:00 2001 From: "m.tre" Date: Wed, 19 Jun 2024 13:17:40 +0500 Subject: [PATCH 5/6] review fixes --- Vostok.Hosting/VostokHost.cs | 15 +++++++++------ Vostok.Hosting/VostokHost_Extensions.cs | 4 +++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Vostok.Hosting/VostokHost.cs b/Vostok.Hosting/VostokHost.cs index 2f1970a1..e0ac7848 100644 --- a/Vostok.Hosting/VostokHost.cs +++ b/Vostok.Hosting/VostokHost.cs @@ -55,7 +55,7 @@ public class VostokHost private volatile VostokHostingEnvironment environment; private volatile ILog log; #if NET6_0_OR_GREATER - private volatile PosixSignalRegistration? sigtermRegistration; + private volatile PosixSignalRegistration sigtermRegistration; #endif public VostokHost([NotNull] VostokHostSettings settings) @@ -177,13 +177,16 @@ public Task StopAsync(bool ensureSuccess = true) public VostokHost RegisterSigtermCancellation() { #if NET6_0_OR_GREATER - // Saving PosixSignalRegistration to variable, because of IDisposable inheritance, + // Saving PosixSignalRegistration reference to nameof(VostokHost) field, because of IDisposable inheritance, // and GC will collect if not assigned. On Dispose and finalize handler will unregister. - sigtermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, ctx => + if (sigtermRegistration == null) { - ctx.Cancel = true; - this.Stop(false); - }); + sigtermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, ctx => + { + ctx.Cancel = true; + this.Stop(false); + }); + } #else AppDomain.CurrentDomain.ProcessExit += (_, _) => this.Stop(false); #endif diff --git a/Vostok.Hosting/VostokHost_Extensions.cs b/Vostok.Hosting/VostokHost_Extensions.cs index 07718923..d6626bf5 100644 --- a/Vostok.Hosting/VostokHost_Extensions.cs +++ b/Vostok.Hosting/VostokHost_Extensions.cs @@ -48,7 +48,9 @@ public static VostokHost WithConsoleCancellation([NotNull] this VostokHost vosto return vostokHost; } - [Obsolete("Use VostokHost.RegisterSigtermCancellation instead of this")] + /// + /// Makes same as + /// public static VostokHost WithSigtermCancellation([NotNull] this VostokHost vostokHost) => vostokHost.RegisterSigtermCancellation(); } From 5d77d32d8b6f4000cf941486e3a25d5fd20363f8 Mon Sep 17 00:00:00 2001 From: "m.tre" Date: Tue, 25 Jun 2024 15:39:19 +0500 Subject: [PATCH 6/6] bump version --- Vostok.Hosting/Vostok.Hosting.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Vostok.Hosting/Vostok.Hosting.csproj b/Vostok.Hosting/Vostok.Hosting.csproj index 930bebdf..46281fe5 100644 --- a/Vostok.Hosting/Vostok.Hosting.csproj +++ b/Vostok.Hosting/Vostok.Hosting.csproj @@ -6,7 +6,7 @@ Debug;Release - 0.3.69 + 0.3.70 Vostok.Hosting