Skip to content

Commit 990f397

Browse files
committed
Handle killing process on non-Windows
1 parent a2b284d commit 990f397

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

Solutions/Corvus.Testing.AzureFunctions/Corvus/Testing/AzureFunctions/FunctionsController.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Corvus.Testing.AzureFunctions
1313
using System.Net.Http;
1414
using System.Net.NetworkInformation;
1515
using System.Net.Sockets;
16+
using System.Runtime.InteropServices;
1617
using System.Threading;
1718
using System.Threading.Tasks;
1819

@@ -198,12 +199,15 @@ private static void KillProcessAndChildren(int pid)
198199
return;
199200
}
200201

201-
using (var searcher =
202-
new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid))
202+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
203203
{
204-
foreach (ManagementObject mo in searcher.Get().Cast<ManagementObject>())
204+
using (var searcher =
205+
new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid))
205206
{
206-
KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
207+
foreach (ManagementObject mo in searcher.Get().Cast<ManagementObject>())
208+
{
209+
KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
210+
}
207211
}
208212
}
209213

@@ -228,7 +232,14 @@ private static void KillProcessAndChildren(int pid)
228232

229233
try
230234
{
231-
proc.Kill();
235+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
236+
{
237+
proc.Kill();
238+
}
239+
else
240+
{
241+
proc.Kill(entireProcessTree: true);
242+
}
232243
}
233244
catch (Win32Exception x)
234245
when (x.ErrorCode == E_ACCESSDENIED)

0 commit comments

Comments
 (0)