From dceaa8eb46dc1a80f66aaa588664767000c1fb4d Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sun, 3 Apr 2022 23:56:06 +0300 Subject: [PATCH 1/2] Annotate --- .../ref/Microsoft.Extensions.Hosting.Systemd.csproj | 1 + .../src/Microsoft.Extensions.Hosting.Systemd.csproj | 1 + .../src/ServiceState.cs | 4 ++-- .../src/SystemdLifetime.cs | 4 ++-- .../src/SystemdLifetime.netcoreapp.cs | 2 +- .../src/SystemdNotifier.cs | 10 ++++++---- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Hosting.Systemd/ref/Microsoft.Extensions.Hosting.Systemd.csproj b/src/libraries/Microsoft.Extensions.Hosting.Systemd/ref/Microsoft.Extensions.Hosting.Systemd.csproj index 8217ee10af78e9..b6229478e382fd 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Systemd/ref/Microsoft.Extensions.Hosting.Systemd.csproj +++ b/src/libraries/Microsoft.Extensions.Hosting.Systemd/ref/Microsoft.Extensions.Hosting.Systemd.csproj @@ -2,6 +2,7 @@ $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.1 + enable true diff --git a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/Microsoft.Extensions.Hosting.Systemd.csproj b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/Microsoft.Extensions.Hosting.Systemd.csproj index 1e2a601e8065f7..042bf76144d8c2 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/Microsoft.Extensions.Hosting.Systemd.csproj +++ b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/Microsoft.Extensions.Hosting.Systemd.csproj @@ -2,6 +2,7 @@ $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.1 + enable true .NET hosting infrastructure for Systemd Services. diff --git a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/ServiceState.cs b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/ServiceState.cs index 22fe30dfe5527e..5767ba2a769c03 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/ServiceState.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/ServiceState.cs @@ -26,9 +26,9 @@ public struct ServiceState /// /// Create custom ServiceState. /// - public ServiceState(string state) + public ServiceState(string state!!) { - _data = Encoding.UTF8.GetBytes(state ?? throw new ArgumentNullException(nameof(state))); + _data = Encoding.UTF8.GetBytes(state); } /// diff --git a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdLifetime.cs b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdLifetime.cs index af166430ad4be8..e49b00fd49c0e9 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdLifetime.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdLifetime.cs @@ -41,12 +41,12 @@ public Task WaitForStartAsync(CancellationToken cancellationToken) { _applicationStartedRegistration = ApplicationLifetime.ApplicationStarted.Register(state => { - ((SystemdLifetime)state).OnApplicationStarted(); + ((SystemdLifetime)state!).OnApplicationStarted(); }, this); _applicationStoppingRegistration = ApplicationLifetime.ApplicationStopping.Register(state => { - ((SystemdLifetime)state).OnApplicationStopping(); + ((SystemdLifetime)state!).OnApplicationStopping(); }, this); diff --git a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdLifetime.netcoreapp.cs b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdLifetime.netcoreapp.cs index c277fc926fff52..4c8f0ff2feaee4 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdLifetime.netcoreapp.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdLifetime.netcoreapp.cs @@ -8,7 +8,7 @@ namespace Microsoft.Extensions.Hosting.Systemd { public partial class SystemdLifetime { - private PosixSignalRegistration _sigTermRegistration; + private PosixSignalRegistration? _sigTermRegistration; private partial void RegisterShutdownHandlers() { diff --git a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdNotifier.cs b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdNotifier.cs index 0b8738823dc4f0..bf7fdeef46aad2 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdNotifier.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdNotifier.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.Net.Sockets; using System.Runtime.Versioning; @@ -12,19 +13,20 @@ public class SystemdNotifier : ISystemdNotifier { private const string NOTIFY_SOCKET = "NOTIFY_SOCKET"; - private readonly string _socketPath; + private readonly string? _socketPath; public SystemdNotifier() : this(GetNotifySocketPath()) { } // For testing - internal SystemdNotifier(string socketPath) + internal SystemdNotifier(string? socketPath) { _socketPath = socketPath; } /// + [MemberNotNullWhen(true, nameof(_socketPath))] public bool IsEnabled => _socketPath != null; /// @@ -46,9 +48,9 @@ public void Notify(ServiceState state) } } - private static string GetNotifySocketPath() + private static string? GetNotifySocketPath() { - string socketPath = Environment.GetEnvironmentVariable(NOTIFY_SOCKET); + string? socketPath = Environment.GetEnvironmentVariable(NOTIFY_SOCKET); if (string.IsNullOrEmpty(socketPath)) { From 1baf2de706773cf5dbc9fd90c4dc4146edc54155 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Mon, 18 Apr 2022 10:40:26 -0500 Subject: [PATCH 2/2] Fix API compat check by removing MemberNotNullWhen attribute. --- .../src/SystemdNotifier.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdNotifier.cs b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdNotifier.cs index bf7fdeef46aad2..715db9b04cab64 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdNotifier.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Systemd/src/SystemdNotifier.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Diagnostics.CodeAnalysis; using System.Net.Sockets; using System.Runtime.Versioning; @@ -26,7 +25,6 @@ internal SystemdNotifier(string? socketPath) } /// - [MemberNotNullWhen(true, nameof(_socketPath))] public bool IsEnabled => _socketPath != null; /// @@ -39,7 +37,7 @@ public void Notify(ServiceState state) using (var socket = new Socket(AddressFamily.Unix, SocketType.Dgram, ProtocolType.Unspecified)) { - var endPoint = new UnixDomainSocketEndPoint(_socketPath); + var endPoint = new UnixDomainSocketEndPoint(_socketPath!); socket.Connect(endPoint); // It's safe to do a non-blocking call here: messages sent here are much