Skip to content

Fix ProcessThreadTests.TestStartTimeProperty flakiness due to /proc visibility race#125811

Merged
danmoseley merged 4 commits intomainfrom
copilot/fix-process-thread-tests
Mar 20, 2026
Merged

Fix ProcessThreadTests.TestStartTimeProperty flakiness due to /proc visibility race#125811
danmoseley merged 4 commits intomainfrom
copilot/fix-process-thread-tests

Conversation

Copy link
Contributor

Copilot AI commented Mar 20, 2026

TestStartTimeProperty was flaky (especially on Mono/Linux) because after a new LongRunning thread starts, its /proc/self/task/ entry may not be visible immediately. The previous code called p.Refresh() once then used Assert.Single, failing hard if the entry wasn't ready yet.

Description

Replace the single-shot refresh + Assert.Single with a bounded retry loop that re-calls p.Refresh() until the thread appears (up to 10 attempts, 100ms apart):

int newThreadId = GetCurrentThreadId();

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<ProcessThread>().FirstOrDefault(t => t.Id == newThreadId);
}

Assert.True(newThread is not null, $"Thread with id {newThreadId} was not found after retrying.");
Assert.InRange(newThread.StartTime.ToUniversalTime(), curTime - allowedWindow, DateTime.Now.ToUniversalTime() + allowedWindow);
Original prompt

This section details on the original issue you should resolve

<issue_title>ProcessThreadTests.TestStartTimeProperty failure in CI</issue_title>
<issue_description>```
System.Diagnostics.Tests.ProcessThreadTests.TestStartTimeProperty [FAIL]
Assert.Single() Failure: The collection did not contain any matching items
Expected: (predicate expression)
Collection: [System.Diagnostics.ProcessThread, System.Diagnostics.ProcessThread, System.Diagnostics.ProcessThread, System.Diagnostics.ProcessThread, System.Diagnostics.ProcessThread, ···]
Stack Trace:
//src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs(156,0): at System.Diagnostics.Tests.ProcessThreadTests.<>c__DisplayClass4_2.b__0()
/
/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs(2396,0): at System.Threading.Tasks.Task.InnerInvoke()
//src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs(2384,0): at System.Threading.Tasks.Task.<>c.<.cctor>b__292_0(Object obj)
/
/src/libraries/System.Private.CoreLib/src/System/Threading/ExecutionContext.cs(179,0): at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
//src/libraries/System.Private.CoreLib/src/System/Threading/ExecutionContext.cs(203,0): at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
/
/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs(2342,0): at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
/_/src/libraries/System.Diagnostics.Process/tests/ProcessThreadTests.cs(149,0): at System.Diagnostics.Tests.ProcessThreadTests.TestStartTimeProperty()
--- End of stack trace from previous location ---


## Build Information
Build: https://dev.azure.com/dnceng-public/cbb18261-c48f-4abb-8651-8cdcb5474649/_build/results?buildId=755830
Build error leg or test failing: System.Diagnostics.Tests.ProcessThreadTests.TestStartTimeProperty
Pull request: https://github.com/dotnet/runtime/pull/105519
<!-- Error message template  -->
## Error Message

Fill the error message using [step by step known issues guidance](https://github.com/dotnet/arcade/blob/main/Documentation/Projects/Build%20Analysis/KnownIssueJsonStepByStep.md).

<!-- Use ErrorMessage for String.Contains matches. Use ErrorPattern for regex matches (single line/no backtracking). Set BuildRetry to `true` to retry builds with this error. Set ExcludeConsoleLog to `true` to skip helix logs analysis. -->

```json
{
  "ErrorMessage": ["System.Diagnostics.Tests.ProcessThreadTests.TestStartTimeProperty [FAIL]","The collection did not contain any matching items"],
  "ErrorPattern": "",
  "BuildRetry": false,
  "ExcludeConsoleLog": false
}

Known issue validation

Build: 🔎 https://dev.azure.com/dnceng-public/public/_build/results?buildId=755830
Error message validated: [System.Diagnostics.Tests.ProcessThreadTests.TestStartTimeProperty [FAIL] The collection did not contain any matching items]
Result validation: ✅ Known issue matched with the provided build.
Validation performed at: 7/26/2024 12:06:54 AM UTC

Report

Build Definition Test Pull Request
1342756 dotnet/runtime System.Diagnostics.Tests.ProcessThreadTests.TestStartTimeProperty #115996
1332497 dotnet/runtime System.Diagnostics.Tests.ProcessThreadTests.TestStartTimeProperty #125022
1326378 dotnet/runtime System.Diagnostics.Tests.ProcessThreadTests.TestStartTimeProperty #124923
1315488 dotnet/runtime [System.Diagnostics.Tests.ProcessThreadTests.TestStartTimeProperty](https://dev.azure.com/dnceng-public/public/_build/results?buildId=1315488&view=ms.vss-test-web.build...

📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Copilot AI and others added 2 commits March 20, 2026 00:31
…y loop

Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ProcessThreadTests.TestStartTimeProperty failure in CI Fix ProcessThreadTests.TestStartTimeProperty flakiness due to /proc visibility race Mar 20, 2026
Copilot AI requested a review from danmoseley March 20, 2026 00:34
@danmoseley danmoseley requested a review from Copilot March 20, 2026 01:09
@danmoseley danmoseley marked this pull request as ready for review March 20, 2026 01:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces flakiness in ProcessThreadTests.TestStartTimeProperty by handling a race where a newly started thread may not immediately appear in Process.Threads (notably on Linux/Mono), replacing a single-shot refresh + Assert.Single with a bounded retry loop.

Changes:

  • Remove the single p.Refresh() + Assert.Single(...) lookup for the spawned thread.
  • Add a bounded retry loop that refreshes and searches for the thread by ID before asserting.
  • Improve failure behavior by emitting a clearer assertion message when the thread never becomes visible.

- Reword /proc-specific comment to be platform-agnostic
- Use DateTime.UtcNow instead of DateTime.Now.ToUniversalTime()

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@danmoseley danmoseley merged commit 4e54117 into main Mar 20, 2026
78 of 84 checks passed
@danmoseley danmoseley deleted the copilot/fix-process-thread-tests branch March 20, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ProcessThreadTests.TestStartTimeProperty failure in CI

5 participants