From ebe4d2671a7dfc7af419604ad3b1c1e67de510db Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 16 Apr 2026 14:02:23 +0000 Subject: [PATCH 1/4] Pass entireProcessTree: true to Process.Kill on non-NET472 monikers Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/615094b6-7ec8-41b1-8347-e49e13e1ae5b Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com> --- .../EndToEnd/ProcessWrapper.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs b/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs index dcffe58f35db..406b74b94bed 100644 --- a/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs +++ b/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs @@ -74,7 +74,11 @@ private static void KillProcessTreeInternal(int pid) try { Process proc = Process.GetProcessById(pid); +#if NET472 proc.Kill(); +#else + proc.Kill(entireProcessTree: true); +#endif } catch (ArgumentException) { From ddb2308d0dfb6157f480dcabf792843e420783ee Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 16 Apr 2026 14:23:33 +0000 Subject: [PATCH 2/4] Dispose Process instances and simplify non-framework KillProcessTree Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/d5c3e367-f4c5-4012-adca-485145733243 Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com> --- .../EndToEnd/ProcessWrapper.cs | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs b/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs index 406b74b94bed..bbb3a3988a77 100644 --- a/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs +++ b/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs @@ -1,7 +1,7 @@ using System; using System.Diagnostics; using System.IO; -#if NET472 +#if NETFRAMEWORK using System.Management; #endif @@ -47,43 +47,52 @@ public class ProcessWrapper public static void KillProcessTree(int processId) { + Process process; try { - Process process = Process.GetProcessById(processId); - if (process != null && !process.HasExited) - { - KillProcessTreeInternal(processId); - } + process = Process.GetProcessById(processId); } - catch (Exception) + catch (ArgumentException) { + // Process might have already exited. + return; } + using (process) + { + if (!process.HasExited) + { +#if NETFRAMEWORK + KillProcessTreeInternal(process.Id); +#else + process.Kill(entireProcessTree: true); +#endif + } + } } +#if NETFRAMEWORK private static void KillProcessTreeInternal(int pid) { -#if NET472 ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid); ManagementObjectCollection moc = searcher.Get(); foreach (ManagementObject mo in moc) { KillProcessTreeInternal(Convert.ToInt32(mo["ProcessID"])); } -#endif + try { - Process proc = Process.GetProcessById(pid); -#if NET472 - proc.Kill(); -#else - proc.Kill(entireProcessTree: true); -#endif + using (Process proc = Process.GetProcessById(pid)) + { + proc.Kill(); + } } catch (ArgumentException) { - // vramak: Process might have already exited. + // Process might have already exited. } } +#endif } } From 01c83841c68c1301371cf1adcb0e8a2ab37b4e81 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 27 Apr 2026 19:14:02 +0000 Subject: [PATCH 3/4] Swallow exceptions in Kill call and dispose WMI objects in NETFRAMEWORK path Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/f8736af7-340d-4aa2-ab34-2bf81e654ad1 Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com> --- .../EndToEnd/ProcessWrapper.cs | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs b/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs index bbb3a3988a77..2e53139fcae7 100644 --- a/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs +++ b/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs @@ -60,13 +60,22 @@ public static void KillProcessTree(int processId) using (process) { - if (!process.HasExited) + try { + if (!process.HasExited) + { #if NETFRAMEWORK - KillProcessTreeInternal(process.Id); + KillProcessTreeInternal(process.Id); #else - process.Kill(entireProcessTree: true); + process.Kill(entireProcessTree: true); #endif + } + } + catch (InvalidOperationException) + { + } + catch (System.ComponentModel.Win32Exception) + { } } } @@ -74,11 +83,16 @@ public static void KillProcessTree(int processId) #if NETFRAMEWORK private static void KillProcessTreeInternal(int pid) { - ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid); - ManagementObjectCollection moc = searcher.Get(); - foreach (ManagementObject mo in moc) + using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid)) + using (ManagementObjectCollection moc = searcher.Get()) { - KillProcessTreeInternal(Convert.ToInt32(mo["ProcessID"])); + foreach (ManagementObject mo in moc) + { + using (mo) + { + KillProcessTreeInternal(Convert.ToInt32(mo["ProcessID"])); + } + } } try From 060c4a87b43586737dc7f58b6ffa8b9aabfaac79 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 27 Apr 2026 19:53:36 +0000 Subject: [PATCH 4/4] Address review: bare catch and target-typed new for ManagementObjectSearcher Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/85362f61-ef98-46f9-903e-c227d04192d0 Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com> --- .../EndToEnd/ProcessWrapper.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs b/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs index 2e53139fcae7..1d6ad3b9622a 100644 --- a/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs +++ b/test/Microsoft.NET.Sdk.Publish.Tasks.Tests/EndToEnd/ProcessWrapper.cs @@ -71,10 +71,7 @@ public static void KillProcessTree(int processId) #endif } } - catch (InvalidOperationException) - { - } - catch (System.ComponentModel.Win32Exception) + catch { } } @@ -83,7 +80,7 @@ public static void KillProcessTree(int processId) #if NETFRAMEWORK private static void KillProcessTreeInternal(int pid) { - using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid)) + using (ManagementObjectSearcher searcher = new("Select * From Win32_Process Where ParentProcessID=" + pid)) using (ManagementObjectCollection moc = searcher.Get()) { foreach (ManagementObject mo in moc)