Skip to content

Honor STAThread entry point for MSTest execution#8469

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-flaky-test-stathreading
Draft

Honor STAThread entry point for MSTest execution#8469
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-flaky-test-stathreading

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 21, 2026

MSTest only honored explicit <ExecutionThreadApartmentState> configuration, so tests launched from an [STAThread] Main could fall back to MTA thread-pool execution after async continuations. This made the no-runsettings STA acceptance scenario timing-dependent and flaky in Debug.

  • Apartment state resolution
    • Added shared effective apartment-state logic:
      • explicit runsettings/config value wins
      • otherwise [STAThread] on the entry assembly entry point implies STA on Windows
      • non-Windows behavior remains unchanged
ApartmentState? requestedApartmentState =
    MSTestSettings.GetEffectiveExecutionApartmentState();
  • Execution flow

    • Updated both MSTest execution paths to use the effective apartment state:
      • MSTestExecutor.RunTestsFromRightContextAsync
      • TestExecutionManager.DefaultFactoryAsync
  • Coverage

    • Re-enabled the no-runsettings STA acceptance test.
    • Added focused unit coverage for explicit MTA precedence, implicit Windows STA, non-Windows behavior, and non-STA entry points.

Copilot AI self-assigned this May 21, 2026
Copilot AI review requested due to automatic review settings May 21, 2026 14:45
Copilot AI review requested due to automatic review settings May 21, 2026 14:45
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot May 21, 2026 15:04
Copilot AI changed the title [WIP] Fix flaky test for STAThreading on Windows Debug Honor STAThread entry point for MSTest execution May 21, 2026
Copilot AI requested a review from Evangelink May 21, 2026 15:06
Copy link
Copy Markdown
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

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

We explicitly don't await the test when running under STA to preserve the STA thread:

if (usesAppDomains || Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
{
#pragma warning disable VSTHRD103 // Call async methods when in an async method - We cannot do right now because we are crossing app domains.
// TODO: When app domains support is dropped, we can finally always be calling the async version.
// In addition to app domains, if we are STA thread (e.g, because runsettings setting ExecutionApartmentState to STA), we want to preserve that.
// If we await, we could end up in a thread pool thread, which is not what we want.
// Alternatively, if we want to use RunSingleTestAsync for the case of STA, we should have:
// 1. A custom single threaded synchronization context that keeps us in STA.
// 2. Use ConfigureAwait(true).
unitTestResult = testRunner.RunSingleTest(unitTestElement, testContextProperties, remotingMessageLogger);
#pragma warning restore VSTHRD103 // Call async methods when in an async method
}

I'm not sure what exactly is breaking here and at which point do you lose the STA thread.

@Youssef1313
Copy link
Copy Markdown
Member

Okay I think I can see what's going on here.

I think the test is written incorrectly and should be changed to use the MSTest configuration.

We never well supported [STAThread] explicitly on entrypoints and it looks unnecessary to do so, and no user should ever be using the GetAwaiter().GetResult() stuff that exist in this test. The test case is a bad practice that shouldn't be encouraged.

Move the multi-line parameter list onto the line after the call to
ExecuteAsync so StyleCop's SA1116 rule is satisfied. Pure formatting
change.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 23, 2026 15:21
Copy link
Copy Markdown
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 improves MSTest execution on Windows by honoring an [STAThread] entry-point when no explicit <ExecutionThreadApartmentState> is configured, reducing timing-dependent STA/MTA behavior during MSTest runner execution.

Changes:

  • Added centralized “effective apartment state” resolution that prioritizes runsettings, otherwise infers STA on Windows from [STAThread] on the entry point.
  • Updated both MSTest execution paths (VSTest adapter and platform services execution manager) to use the effective apartment state.
  • Re-enabled and adjusted STA acceptance coverage, and added targeted unit tests for precedence and platform behavior.
Show a summary per file
File Description
src/Adapter/MSTestAdapter.PlatformServices/MSTestSettings.cs Adds shared effective apartment-state resolution (runsettings override, otherwise infer STA on Windows from entry point).
src/Adapter/MSTestAdapter.PlatformServices/Execution/TestExecutionManager.cs Uses the effective apartment state when deciding whether to run work on an STA thread.
src/Adapter/MSTest.TestAdapter/VSTestAdapter/MSTestExecutor.cs Uses the effective apartment state when selecting the “right context” for execution.
test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestSettingsTests.cs Adds unit coverage for precedence (configured wins), Windows STA inference, non-Windows behavior, and non-STA entry points.
test/IntegrationTests/MSTest.Acceptance.IntegrationTests/STAThreadingTests.cs Re-enables and strengthens STA entry-point acceptance scenario (no-runsettings case) and adjusts ExecuteAsync invocation formatting/inputs.

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 0

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.

Flaky test: TestMethodThreading_MainIsSTAThread_OnWindows_NoRunsettingsProvided_ThreadIsSTA fails reliably on Windows Debug

4 participants