Skip to content

Commit 7977b6c

Browse files
committed
Tweak dotnet cleanup logic
1 parent 268a1c4 commit 7977b6c

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

Runner/Helpers/DotnetHelpers.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,35 @@ internal static class DotnetHelpers
44
{
55
public static async Task KillRemainingDotnetProcessesAsync(JobBase job)
66
{
7-
string[] processNames = ["dotnet", "MSBuild", "corerun", "superpmi", "test_fx_ver", ".NET Host"];
8-
9-
if (OperatingSystem.IsWindows())
7+
foreach (Process proc in Process.GetProcesses())
108
{
11-
foreach (string processName in processNames)
9+
try
1210
{
13-
await job.RunProcessAsync("taskkill", $"/IM \"{processName}.exe\" /F", logPrefix: "Cleanup .NET processes", checkExitCode: false);
11+
string name = proc.ProcessName;
12+
13+
if (name.Contains("dotnet", StringComparison.OrdinalIgnoreCase) ||
14+
name.Contains("MSBuild", StringComparison.OrdinalIgnoreCase) ||
15+
name.Contains("corerun", StringComparison.OrdinalIgnoreCase) ||
16+
name.Contains("superpmi", StringComparison.OrdinalIgnoreCase) ||
17+
name.Contains("test_fx_ver", StringComparison.OrdinalIgnoreCase) ||
18+
name.Contains(".NET Host", StringComparison.OrdinalIgnoreCase))
19+
{
20+
if (proc.Id == Environment.ProcessId)
21+
{
22+
continue;
23+
}
24+
25+
await job.LogAsync($"Killing process {proc.Id} ({proc.ProcessName})");
26+
proc.Kill(entireProcessTree: true);
27+
}
1428
}
15-
}
16-
else
17-
{
18-
foreach (string processName in processNames)
29+
catch (Exception ex)
30+
{
31+
await job.LogAsync($"Failed to kill process {proc.Id} ({proc.ProcessName}): {ex}");
32+
}
33+
finally
1934
{
20-
await job.RunProcessAsync("pkill", $"-f \"{processName}\"", logPrefix: "Cleanup .NET processes", checkExitCode: false);
35+
proc.Dispose();
2136
}
2237
}
2338
}

Runner/Runner.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<File Path="../.github/workflows/publish-docker.yml" />
44
<File Path="../.github/workflows/run-script.yml" />
55
<File Path="../azure-pipelines.yml" />
6+
<File Path="../NuGet.config" />
67
</Folder>
78
<Project Path="Runner.csproj" />
89
</Solution>

0 commit comments

Comments
 (0)