Simplify and fix ProcessWrapper.KillProcessTree#53919
Open
Conversation
Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/615094b6-7ec8-41b1-8347-e49e13e1ae5b Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
adamsitnik
April 16, 2026 14:05
View session
adamsitnik
reviewed
Apr 16, 2026
Member
There was a problem hiding this comment.
@copilot I believe there is still place for improvement:
- we need to ensure every
Processinstance gets disposed - we can simplify the non-framework impl
Following code is pseudocode, please take as inspiration, make sure you can build the product before pushing any changes.
public static void KillProcessTree(int processId)
{
using Process? process = null;
try
{
process = Process.GetProcessById(processId);
}
catch (ArgumentException)
{
// Process might have already exited.
return;
}
if (!process.HasExited)
{
#if NETFRAMEWORK
KillProcessTreeInternal(process.ID);
#else
process.Kill(entireProcessTree: true);
#endif
}
}
#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)
{
KillProcessTreeInternal(Convert.ToInt32(mo["ProcessID"]));
}
try
{
using Process proc = Process.GetProcessById(pid);
proc.Kill();
}
catch (ArgumentException)
{
// vramak: Process might have already exited.
}
}
#endifAgent-Logs-Url: https://github.com/dotnet/sdk/sessions/d5c3e367-f4c5-4012-adca-485145733243 Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Contributor
Author
Applied both improvements in ddb2308:
|
Copilot
AI
changed the title
Use Process.Kill(entireProcessTree: true) on non-NET472 in ProcessWrapper
Simplify and fix ProcessWrapper.KillProcessTree
Apr 16, 2026
This was referenced Apr 16, 2026
This was referenced Apr 24, 2026
Open
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the ProcessWrapper test helper to use the built-in process-tree termination API on modern .NET, while keeping the existing WMI-based recursive kill for .NET Framework.
Changes:
- On non-.NET Framework targets, uses
Process.Kill(entireProcessTree: true)directly fromKillProcessTree. - Restricts
KillProcessTreeInternal(WMI recursion) to#if NETFRAMEWORK. - Ensures
Processinstances are disposed viausing.
…RK 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>
adamsitnik
reviewed
Apr 27, 2026
Member
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot address my feedback
…earcher Agent-Logs-Url: https://github.com/dotnet/sdk/sessions/85362f61-ef98-46f9-903e-c227d04192d0 Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Contributor
Author
adamsitnik
reviewed
Apr 28, 2026
Member
adamsitnik
left a comment
There was a problem hiding this comment.
LGTM, @vijayrkn the PR is ready for review
adamsitnik
approved these changes
Apr 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
KillProcessTreeInternalcallsProcess.Kill()without theentireProcessTreeparameter, missing the native process-tree kill available since .NET Core 3.0. On NETFRAMEWORK, the method manually walks child processes via WMI — that path is unchanged.process.Kill(entireProcessTree: true)directly inKillProcessTree, eliminating the need forKillProcessTreeInternalKillProcessTreeInternalbehind#if NETFRAMEWORKsince it is only needed for the WMI-based recursive child process killProcessinstances are properly disposed viausingManagementObjectSearcher,ManagementObjectCollection,ManagementObject) in the NETFRAMEWORK path to prevent handle leaks