Skip to content

Commit 93d8322

Browse files
authored
Merge pull request #8968 from tmds/process_kill
ProcessExtensions: use BCL Process.Kill.
2 parents c6277a4 + 608a496 commit 93d8322

File tree

1 file changed

+6
-78
lines changed

1 file changed

+6
-78
lines changed

src/Shared/ProcessExtensions.cs

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
5-
using System.Collections.Generic;
64
using System.Diagnostics;
7-
using System.IO;
85

96
#nullable disable
107

@@ -14,100 +11,31 @@ internal static class ProcessExtensions
1411
{
1512
public static void KillTree(this Process process, int timeoutMilliseconds)
1613
{
14+
#if NETCOREAPP
15+
process.Kill(entireProcessTree: true);
16+
#else
1717
if (NativeMethodsShared.IsWindows)
1818
{
1919
try
2020
{
2121
// issue the kill command
2222
NativeMethodsShared.KillTree(process.Id);
2323
}
24-
catch (InvalidOperationException)
24+
catch (System.InvalidOperationException)
2525
{
2626
// The process already exited, which is fine,
2727
// just continue.
2828
}
2929
}
3030
else
3131
{
32-
var children = new HashSet<int>();
33-
GetAllChildIdsUnix(process.Id, children);
34-
foreach (var childId in children)
35-
{
36-
KillProcessUnix(childId);
37-
}
38-
39-
KillProcessUnix(process.Id);
32+
throw new System.NotSupportedException();
4033
}
41-
34+
#endif
4235
// wait until the process finishes exiting/getting killed.
4336
// We don't want to wait forever here because the task is already supposed to be dieing, we just want to give it long enough
4437
// to try and flush what it can and stop. If it cannot do that in a reasonable time frame then we will just ignore it.
4538
process.WaitForExit(timeoutMilliseconds);
4639
}
47-
48-
private static void GetAllChildIdsUnix(int parentId, ISet<int> children)
49-
{
50-
RunProcessAndWaitForExit(
51-
"pgrep",
52-
$"-P {parentId}",
53-
out string stdout);
54-
55-
if (!string.IsNullOrEmpty(stdout))
56-
{
57-
using (var reader = new StringReader(stdout))
58-
{
59-
while (true)
60-
{
61-
var text = reader.ReadLine();
62-
if (text == null)
63-
{
64-
return;
65-
}
66-
67-
int id;
68-
if (int.TryParse(text, out id))
69-
{
70-
children.Add(id);
71-
// Recursively get the children
72-
GetAllChildIdsUnix(id, children);
73-
}
74-
}
75-
}
76-
}
77-
}
78-
79-
private static void KillProcessUnix(int processId)
80-
{
81-
try
82-
{
83-
using Process process = Process.GetProcessById(processId);
84-
process.Kill();
85-
}
86-
catch (ArgumentException)
87-
{
88-
// Process already terminated.
89-
return;
90-
}
91-
catch (InvalidOperationException)
92-
{
93-
// Process already terminated.
94-
return;
95-
}
96-
}
97-
98-
private static void RunProcessAndWaitForExit(string fileName, string arguments, out string stdout)
99-
{
100-
var startInfo = new ProcessStartInfo
101-
{
102-
FileName = fileName,
103-
Arguments = arguments,
104-
RedirectStandardOutput = true,
105-
UseShellExecute = false
106-
};
107-
108-
var process = Process.Start(startInfo);
109-
stdout = process.StandardOutput.ReadToEnd();
110-
process.WaitForExit();
111-
}
11240
}
11341
}

0 commit comments

Comments
 (0)