@@ -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 }
0 commit comments