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(); }