diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs index 6c97c4424f6ba1..0f9965fb8b9b24 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs @@ -148,14 +148,19 @@ public async Task TestStartTimeProperty() // that there's at least one thread greater than the current time we previously grabbed. await Task.Factory.StartNew(() => { - p.Refresh(); - int newThreadId = GetCurrentThreadId(); - ProcessThread[] processThreads = p.Threads.Cast().ToArray(); - ProcessThread newThread = Assert.Single(processThreads, thread => thread.Id == newThreadId); + // Retry to handle the race where the newly started thread is not yet visible via Process.Threads. + ProcessThread newThread = null; + for (int i = 0; i < 10 && newThread is null; i++) + { + if (i > 0) Thread.Sleep(100); + p.Refresh(); + newThread = p.Threads.Cast().FirstOrDefault(t => t.Id == newThreadId); + } - Assert.InRange(newThread.StartTime.ToUniversalTime(), curTime - allowedWindow, DateTime.Now.ToUniversalTime() + allowedWindow); + Assert.True(newThread is not null, $"Thread with id {newThreadId} was not found after retrying."); + Assert.InRange(newThread.StartTime.ToUniversalTime(), curTime - allowedWindow, DateTime.UtcNow + allowedWindow); }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); } }