Repository: dotnet/runtime
File: src/tests/tracing/userevents/common/UserEventsTestRunner.cs (C#)

Code before:
                {
                    foreach (FileInfo fi in ipc)
                    {
                        try
                        {
                            Console.WriteLine($"Deleting zombie diagnostic port: {fi.FullName}");
                            fi.Delete();
                        }
                        catch (UnauthorizedAccessException)
                        {
                            Console.WriteLine($"Skipping zombie diagnostic port (permission denied): {fi.FullName}");
                        }
                        catch (IOException)
                        {
                            Console.WriteLine($"Skipping zombie diagnostic port (I/O error): {fi.FullName}");
                        }
                    }
                }
                else
                {
                    if (ipc.Count() > 1)
                    {
                        // delete zombied pipes except newest which is owned
                        var duplicates = ipc.OrderBy(fileInfo => fileInfo.CreationTime.Ticks).SkipLast(1);
                        foreach (FileInfo fi in duplicates)
                        {
                            try
                            {
                                Console.WriteLine($"Deleting duplicate diagnostic port: {fi.FullName}");
                                fi.Delete();
                            }
                            catch (UnauthorizedAccessException)
                            {
                                Console.WriteLine($"Skipping duplicate diagnostic port (permission denied): {fi.FullName}");
                            }
                            catch (IOException)
                            {
                                Console.WriteLine($"Skipping duplicate diagnostic port (I/O error): {fi.FullName}");
                            }
                        }
                    }
                }
            }
        }

        private static void UploadTraceFileFromHelix(string traceFilePath, string scenarioName)
        {
            var helixWorkItemDirectory = Environment.GetEnvironmentVariable("HELIX_WORKITEM_UPLOAD_ROOT");
            if (helixWorkItemDirectory != null && Directory.Exists(helixWorkItemDirectory))
            {
                var destPath = Path.Combine(helixWorkItemDirectory, $"{scenarioName}.nettrace");

Review comment (suggestion):
Do you know why we need to do this at all? In theory the runtime should do a good enough job cleaning up after itself rather than relying on external tools to do it. I don't think we need to address it in this PR but I suspect either the test is doing needless work or the test is actively masking a runtime issue.

Code after:
                    .Select(filename =>
                    {
                        var match = Regex.Match(filename, @"dotnet-diagnostic-(?<pid>\d+)");
                        if (match.Success && match.Groups["pid"].Success && !string.IsNullOrEmpty(match.Groups["pid"].Value))
                        {
                            return new { pid = int.Parse(match.Groups["pid"].Value), fileInfo = new FileInfo(filename) };
                        }
                        return null;
                    })
                    .Where(fileInfoGroup => fileInfoGroup is not null)
                    .GroupBy(fileInfos => fileInfos.pid, fileInfos => fileInfos.fileInfo);
                List<int> currentPids = System.Diagnostics.Process.GetProcesses().Select(pid => pid.Id).ToList();
                return (currentIpcs, currentPids);
            };

            var (currentIpcs, currentPids) = GetPidsAndSockets();

            foreach (var ipc in currentIpcs)
            {
                if (!currentPids.Contains(ipc.Key))
                {
                    foreach (FileInfo fi in ipc)
                    {
                        try
                        {
                            Console.WriteLine($"Deleting zombie diagnostic port: {fi.FullName}");
                            fi.Delete();
                        }
                        catch (UnauthorizedAccessException)
                        {
                            Console.WriteLine($"Skipping zombie diagnostic port (permission denied): {fi.FullName}");
                        }
                        catch (IOException)
                        {
                            Console.WriteLine($"Skipping zombie diagnostic port (I/O error): {fi.FullName}");
                        }
                    }
                }
                else
                {
                    if (ipc.Count() > 1)
                    {
                        // delete zombied pipes except newest which is owned
                        var duplicates = ipc.OrderBy(fileInfo => fileInfo.CreationTime.Ticks).SkipLast(1);
                        foreach (FileInfo fi in duplicates)
                        {
                            try
                            {
                                Console.WriteLine($"Deleting duplicate diagnostic port: {fi.FullName}");
                                fi.Delete();
                            }