Skip to content

fix(winforms): isolate WinForms test message loops and initialize the toolkit once - #3

Merged
fffonion merged 1 commit into
masterfrom
subagent/ironrust-ci-fix-20261117-c3c6e326
Nov 16, 2026
Merged

fffonion merged 1 commit into
masterfrom
subagent/ironrust-ci-fix-20261117-c3c6e326

Conversation

@fffonion

Copy link
Copy Markdown
Contributor

Problem

Release run 43890270237 (Package win-x64 → Test managed projects, HEAD 85b4e16) failed with 83/84 passing:

Failed PdVm.Tests.PdVmTypedDotNetInteropTests.WinFormsButtonClickInvokesRssCallbackOnTheOwningStaThread [3 s]
  Assert.Equal() Failure: Strings differ
Expected: "updated"
Actual:   null

threadError was null and the signal file was never written: the click callback never ran and the form was never closed, yet nothing reported an error. The same test passed in the IronRust CI windows job on the same commit (taking ~2 s instead of ~80 ms), so the failure is ordering/timing dependent.

Root cause

The three Windows Forms test classes (PdVmTypedDotNetInteropTests, PdVmDotNetHostTests, PdVmMinesweeperTests) ran in parallel while sharing process-global Windows Forms state:

  1. Application.Exit() was used as the per-test watchdog. Exit tears down every message loop in the process, not just the caller's. A watchdog firing in one test therefore terminated another test's Application.Run – the click test then lost its Shown/Click dispatch, its own watchdog fired 3 s later, and the callback never posted. Reproduced directly: with two message loops live in one process, a 3 s Exit in test A ended test B's loop at 3567 ms with its Shown signal still missing, and also failed PdVmDotNetHostTests.ConstructsWindowsFormOnStaThreadWithoutCompileTimeWindowsDependency (8 ms).
  2. One-shot toolkit initialization raced. Every test called Application.SetHighDpiMode/EnableVisualStyles, which only take effect before the first window handle exists. Once any test had created a form, PdVmDotNetHost.InitializeWindowsFormsApplication() returned false (or ran concurrently with the first form), even though the toolkit was fine.
  3. Unbounded first-use cost sat inside a fixed deadline. Measured per-iteration on windows-latest: first Application.Run in the process took ~2–3.5 s (assembly load, GDI+/fonts/themes, first window) versus 18–31 ms for later iterations. That cost was inside the test's 3 s watchdog, so the watchdog could fire before the interaction even started.

Fix

  • Add a WinFormsCollection (DisableParallelization = true) and put every form-creating test class in it, so Windows Forms tests never run concurrently.
  • Add WinFormsTestSupport: initializes Windows Forms once per process (thread-safe) and pumps a throwaway message loop so one-time toolkit startup happens outside any test's deadline. All tests now assert against that single initialization result.
  • Add PdVmWinFormsApplicationSession.CloseMainForm(): closes the session's own main form on its UI thread (through the same path as EventLoop::Close, so a bound FormClosing cannot veto it) instead of Application.Exit(). The two message-loop tests use it for their watchdog, so a timeout terminates only the loop under test.
  • Remove the process-wide ExitWindowsFormsApplication helper.

Test semantics are unchanged: the click must invoke the RSS callback, the label must read updated, and the form must close. No assertion was relaxed, removed, or skipped, and no timeout was raised.

Verification

  • IronRust CI 43923832143 – windows-latest: 84/84 passed, 0 warnings; WinFormsButtonClickInvokesRssCallbackOnTheOwningStaThread now 82 ms (was 2–3 s).
  • Release 43925319854 (workflow_dispatch on this branch, the exact job that failed): Package win-x64 → Test managed projects 84/84 passed, package step succeeded.
  • Linux (this branch): dotnet test PdVm.Tests -c Release → 84/84 passed; the WinForms tests return early on non-Windows, so real UI coverage is the Windows jobs above.
  • cargo test --locked (native compiler): 8 + 9 tests passed.

Files

  • PdVm.Runtime/PdVmWinFormsApplication.cs – CloseMainForm(), volatile main-form publication.
  • PdVm.Tests/WinFormsTestSupport.cs (new) – non-parallel collection, one-time initialization and warm-up.
  • PdVm.Tests/PdVmTypedDotNetInteropTests.cs, PdVm.Tests/PdVmDotNetHostTests.cs, PdVm.Tests/PdVmMinesweeperTests.cs – collection membership, shared initialization, loop-local watchdog.

Based on master @ 85b4e16 (the HEAD of the failing run).

…once

Windows managed tests failed in the v0.3.0 Release build with
PdVm.Tests.PdVmTypedDotNetInteropTests.WinFormsButtonClickInvokesRssCallbackOnTheOwningStaThread
asserting "updated" but getting null: the click callback never ran and the
form never closed.

The WinForms test classes ran concurrently and shared process-global Windows
Forms state:

* every test called Application.EnableVisualStyles/SetHighDpiMode, but those
  only take effect before the first window handle exists, so a later test saw
  InitializeWindowsFormsApplication() return false or raced the first form;
* the 3s watchdog called Application.Exit(), which tears down every message
  loop in the process, so one test's timeout killed the other tests' loops -
  the click test then lost Shown/Click dispatch and reported no signal;
* first-use toolkit initialization (assembly load, GDI+/fonts/themes, first
  Application.Run) took ~2-3.5s on the runner and landed inside that 3s
  deadline, so the watchdog fired before the callback chain completed.

Put the WinForms tests in one non-parallel collection, initialize Windows
Forms once per process (with a throwaway message loop outside any test
deadline), and give the session a CloseMainForm() that closes only its own
form instead of exiting every loop in the process. Test semantics are
unchanged: the button click must invoke the RSS callback, update the label
to "updated" and close the form.
@fffonion
fffonion merged commit 9b7f2f2 into master Nov 16, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant