From cbeffaf651e5ba71f6354f9963c545c7e09afac7 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 10 Sep 2024 20:37:54 +1000 Subject: [PATCH] add missing dispose for process instances --- src/DotNetWorker.Core/StartupHook.cs | 3 ++- src/DotNetWorker.Core/WorkerInformation.cs | 9 ++++++++- .../FunctionsResourceDetector.cs | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/DotNetWorker.Core/StartupHook.cs b/src/DotNetWorker.Core/StartupHook.cs index 18fe2fd86..6bb367948 100644 --- a/src/DotNetWorker.Core/StartupHook.cs +++ b/src/DotNetWorker.Core/StartupHook.cs @@ -35,7 +35,8 @@ public static void Initialize() #if NET5_0_OR_GREATER int processId = Environment.ProcessId; #else - int processId = Process.GetCurrentProcess().Id; + using var process = Process.GetCurrentProcess(); + int processId = process.Id; #endif static bool WaitOnDebugger(int cycle) diff --git a/src/DotNetWorker.Core/WorkerInformation.cs b/src/DotNetWorker.Core/WorkerInformation.cs index 843e10021..37daa2fc5 100644 --- a/src/DotNetWorker.Core/WorkerInformation.cs +++ b/src/DotNetWorker.Core/WorkerInformation.cs @@ -25,7 +25,14 @@ internal sealed class WorkerInformation public string RuntimeIdentifier => RuntimeInformation.RuntimeIdentifier; #else - public int ProcessId => Process.GetCurrentProcess().Id; + public int ProcessId + { + get + { + using var process = Process.GetCurrentProcess(); + return process.Id; + } + } public string RuntimeIdentifier => "n/a"; // Resolve in netstandard #endif diff --git a/src/DotNetWorker.OpenTelemetry/FunctionsResourceDetector.cs b/src/DotNetWorker.OpenTelemetry/FunctionsResourceDetector.cs index 93c476bec..b6b800462 100644 --- a/src/DotNetWorker.OpenTelemetry/FunctionsResourceDetector.cs +++ b/src/DotNetWorker.OpenTelemetry/FunctionsResourceDetector.cs @@ -23,7 +23,8 @@ public Resource Detect() attributeList.Add(new KeyValuePair(ResourceSemanticConventions.ServiceVersion, version)); attributeList.Add(new KeyValuePair(ResourceSemanticConventions.AISDKPrefix, $@"{OpenTelemetryConstants.SDKPrefix}:{typeof(FunctionsResourceDetector).Assembly.GetName().Version}")); - attributeList.Add(new KeyValuePair(ResourceSemanticConventions.ProcessId, Process.GetCurrentProcess().Id)); + using var process = Process.GetCurrentProcess(); + attributeList.Add(new KeyValuePair(ResourceSemanticConventions.ProcessId, process.Id)); // Add these attributes only if running in Azure. if (!string.IsNullOrEmpty(serviceName))