Skip to content

Fix flaky MainWindow tests by replacing notepad.exe with a deterministic RemoteExecutor helper - #125907

Merged
adamsitnik merged 5 commits into
mainfrom
copilot/fix-main-window-handle-test
Mar 26, 2026
Merged

adamsitnik merged 5 commits into
mainfrom
copilot/fix-main-window-handle-test

Conversation

Copilot AI commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor

main PR

Description

Windows 11's modern Notepad uses an App Execution Alias: Process.Start("notepad.exe") returns a stub launcher PID, not the PID that owns the Notepad window. MainWindowFinder uses EnumWindows + GetWindowThreadProcessId to match windows to the target PID — so it always returns IntPtr.Zero for the stub, regardless of retries.

Fix: Replace the notepad dependency with a RemoteExecutor child process that calls MessageBoxW(IntPtr.Zero, ...) on a background thread, creating a visible, owner-less top-level window on a controlled PID. This satisfies both MainWindowFinder.IsMainWindow criteria (GetWindow(GW_OWNER) == NULL and IsWindowVisible), and the window title ("Test Main Window") makes MainWindowTitle non-empty.

  • ProcessTests.cs — removed both failing test methods
  • ProcessTests.Windows.cs — added:
    • IsNotNanoServerNotServerCoreAndRemoteExecutorSupported combined condition (preserves existing GUI guards, adds RemoteExecutor.IsSupported)
    • CreateMainWindowWithTitle() static helper: shows MessageBoxW on a background thread (with an explanatory comment noting that MessageBoxW blocks the calling thread), uses FindWindowW to spin-poll until the window is visible, writes "ready" to stdout, then blocks on Thread.Sleep(Timeout.Infinite) until killed by the parent — no race condition
    • Both tests merged into a single [ConditionalFact] MainWindowHandle_And_Title_GetWithGui_ShouldRefresh_Windows (declared async Task) that checks both MainWindowHandle and MainWindowTitle in one pass, reducing sequential test run time
    • Parent uses CreateProcess(CreateMainWindowWithTitle) from ProcessTestBase (which calls AddProcessForDispose internally) and sets RedirectStandardOutput = true before calling process.Start(); waits once via await process.StandardOutput.ReadLineAsync(), performs one Refresh(), then asserts MainWindowHandle != IntPtr.Zero and Assert.Equal("Test Main Window", MainWindowTitle) — no retry polling loop, no manual handle-steal pattern

Customer Impact

Tests fail consistently (~daily) on every CI run targeting full Windows editions. No production code change; test-only impact.

Regression

Not a regression in production code. The test began failing when Windows 11 shipped the new Notepad with App Execution Alias support.

Testing

The new helper is fully deterministic: the child signals the parent only after FindWindowW confirms the window is visible, eliminating the previous polling race. The parent awaits that signal once (no retry loop), performs a single Refresh(), and asserts both MainWindowHandle and MainWindowTitle in one merged async fact. The process is killed explicitly and cleaned up via AddProcessForDispose.

Risk

Low. Test-only change. No production code modified. The approach is consistent with existing patterns in the test suite (stdout-based synchronization mirrors the signal tests; CreateProcess from ProcessTestBase handles cleanup registration automatically). The skip conditions are preserved for Nano Server and Server Core.

Package authoring no longer needed in .NET 9

IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.

Original prompt

This section details on the original issue you should resolve

<issue_title>[8.0] Test failure in System.Diagnostics.Tests.ProcessTests.MainWindowHandle_GetWithGui_ShouldRefresh_Windows </issue_title>
<issue_description>## Build Information
Build: https://dev.azure.com/dnceng-public/cbb18261-c48f-4abb-8651-8cdcb5474649/_build/results?buildId=659152
Build error leg or test failing: System.Diagnostics.Tests.ProcessTests.MainWindowTitle_GetWithGui_ShouldRefresh_Windows
Pull request: #101098

Error Message

Fill the error message using step by step known issues guidance.

{
  "ErrorMessage": "ProcessTests.cs(1782,0): at System.Diagnostics.Tests.ProcessTests.MainWindowHandle_GetWithGui_ShouldRefresh_Windows",
  "ErrorPattern": "",
  "BuildRetry": false,
  "ExcludeConsoleLog": false
}
    System.Diagnostics.Tests.ProcessTests.MainWindowHandle_GetWithGui_ShouldRefresh_Windows [FAIL]
      Assert.NotEqual() Failure
      Expected: Not 0
      Actual:   0
      Stack Trace:
        /_/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs(1782,0): at System.Diagnostics.Tests.ProcessTests.MainWindowHandle_GetWithGui_ShouldRefresh_Windows()
           at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
        /_/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.CoreCLR.cs(36,0): at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
        /_/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs(57,0): at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
    System.Diagnostics.Tests.ProcessTests.MainWindowTitle_GetWithGui_ShouldRefresh_Windows [FAIL]
      Assert.NotEqual() Failure
      Expected: Not ""
      Actual:   ""
      Stack Trace:
        /_/src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs(1817,0): at System.Diagnostics.Tests.ProcessTests.MainWindowTitle_GetWithGui_ShouldRefresh_Windows()
           at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
        /_/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.CoreCLR.cs(36,0): at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
        /_/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs(57,0): at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Known issue validation

Build: 🔎 https://dev.azure.com/dnceng-public/public/_build/results?buildId=659152
Error message validated: [ProcessTests.cs(1782,0): at System.Diagnostics.Tests.ProcessTests.MainWindowHandle_GetWithGui_ShouldRefresh_Windows]
Result validation: ✅ Known issue matched with the provided build.
Validation performed at: 4/29/2024 10:22:00 PM UTC

Report

Build Definition Test Pull Request
1346206 dotnet/runtime System.Diagnostics.Tests.ProcessTests.MainWindowHandle_GetWithGui_ShouldRefresh_Windows
1346205 dotnet/runtime System.Diagnostics.Tests.ProcessTests.MainWindowHandle_GetWithGui_ShouldRefresh_Windows
1344742 dotnet/runtime System.Diagnostics.Tests.ProcessTests.MainWindowHandle_GetWithGui_ShouldRefresh_Windows
1344743 dotnet/runtime...

⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

Copilot AI changed the title [WIP] Fix test failure in MainWindowHandle_GetWithGui_ShouldRefresh_Windows Fix flaky MainWindow tests by replacing notepad.exe with a deterministic RemoteExecutor helper Mar 22, 2026
Copilot AI requested a review from danmoseley March 22, 2026 04:01
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

@danmoseley
danmoseley marked this pull request as ready for review March 22, 2026 05:11
Copilot AI review requested due to automatic review settings March 22, 2026 05:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 addresses flakiness in System.Diagnostics.Process Windows GUI tests caused by Windows 11 Notepad returning a launcher/stub PID that doesn’t own the main window, breaking MainWindowFinder’s PID-to-window matching.

Changes:

  • Removed the two Notepad-based MainWindowHandle/MainWindowTitle refresh tests from ProcessTests.cs.
  • Reintroduced the tests in ProcessTests.Windows.cs using a deterministic RemoteExecutor child process that shows a MessageBoxW with a known caption.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs Removes the Notepad-dependent GUI tests that are unreliable on Windows 11 due to alias/stub PID behavior.
src/libraries/System.Diagnostics.Process/tests/ProcessTests.Windows.cs Adds a deterministic RemoteExecutor + MessageBoxW helper and rewrites the GUI refresh tests to target a controlled PID/window pair.

@danmoseley
danmoseley requested a review from adamsitnik March 22, 2026 05:44
@danmoseley

Copy link
Copy Markdown
Contributor

@copilot address feedback

Copilot AI commented Mar 22, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address feedback

Both test methods now use CreateProcess(CreateMainWindowWithTitle) from ProcessTestBase, which internally calls AddProcessForDispose to register the process for automatic cleanup. This ensures the Process handle is properly tracked and disposed even if Start() throws. Done in 808567e.

Copilot AI requested a review from danmoseley March 22, 2026 07:28

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot it's the right direction but please address my feedback before we move forward

Copilot AI commented Mar 22, 2026

Copy link
Copy Markdown
Contributor Author

@copilot it's the right direction but please address my feedback before we move forward

Done in fb68fb2 — all four points addressed: merged tests, stdout-based sync, no retry loop, exact title assertion.

Copilot AI requested a review from adamsitnik March 22, 2026 10:36
@danmoseley

Copy link
Copy Markdown
Contributor

@copilot is all feedback adddressed if so resolve it

Copilot AI commented Mar 23, 2026

Copy link
Copy Markdown
Contributor Author

@copilot is all feedback adddressed if so resolve it

Yes, all feedback has been addressed in fb68fb2. All four @adamsitnik threads are resolved — the tests are merged into one fact, stdout-based synchronization replaces polling, the child blocks until killed, and the title assertion is exact.

@danmoseley danmoseley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks clean to me

@danmoseley

Copy link
Copy Markdown
Contributor

@adamsitnik mergeable?

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot please address my feedback

Copilot AI commented Mar 25, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please address my feedback

Done in e0969a1 — added the explanatory comment on the thread, and simplified the test to use CreateProcess() + using + await ReadLineAsync() directly.

Copilot AI requested a review from adamsitnik March 25, 2026 14:17
@adamsitnik

Copy link
Copy Markdown
Member

/azp list

@azure-pipelines

Copy link
Copy Markdown
CI/CD Pipelines for this repository:

@adamsitnik

Copy link
Copy Markdown
Member

/azp run runtime-libraries-coreclr outerloop-windows

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM the outerloop windows tests for process are going to be green.

Thank you @danmoseley !

@danmoseley

Copy link
Copy Markdown
Contributor

/ba-g _cffi_backend' i already opened issue for

@adamsitnik
adamsitnik merged commit a4bfa0d into main Mar 26, 2026
89 of 95 checks passed
@adamsitnik
adamsitnik deleted the copilot/fix-main-window-handle-test branch March 26, 2026 03:42
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[8.0] Test failure in System.Diagnostics.Tests.ProcessTests.MainWindowHandle_GetWithGui_ShouldRefresh_Windows

4 participants