Extract duplicate STA thread dispatch pattern into StaThreadHelper#9305
Open
Copilot wants to merge 3 commits into
Open
Extract duplicate STA thread dispatch pattern into StaThreadHelper#9305Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
Reduces ~80 lines of duplicate code across TestClassInfo.Initializer.cs and TestClassInfo.Cleanup.cs into a single shared helper method StaThreadHelper.RunOnStaThreadIfNeededAsync<TResult> in the Helpers folder. The helper handles: - Checking if STA thread is needed (needsSta && Windows && !already STA) - Creating and starting a named STA thread - Joining the thread with error logging on failure - Logging a warning when STA is requested on non-Windows - Falling back to direct await when STA is not needed Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
…dHelper - Narrow catch to ThreadStateException|ThreadInterruptedException as those are the only exception types Thread.Join() can throw - Include thread name in error log message for better diagnostics - Extract default TestResult to a local variable in Initializer.cs to reduce line length Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor duplicate STA thread dispatch code in MSTest adapter
Extract duplicate STA thread dispatch pattern into StaThreadHelper
Jun 21, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces duplication in the MSTest adapter’s STA dispatch logic by centralizing the “run on a dedicated STA thread when required” pattern into a single helper, and updating ClassInitialize/ClassCleanup to use it.
Changes:
- Added
StaThreadHelper.RunOnStaThreadIfNeededAsync<TResult>to encapsulate STA thread creation, start, join, and non-Windows warning behavior. - Replaced duplicated STA dispatch blocks in
TestClassInfo.Initializer.csandTestClassInfo.Cleanup.cswith a single helper call each. - Standardized Join() exception handling/logging in the extracted helper.
Show a summary per file
| File | Description |
|---|---|
| src/Adapter/MSTestAdapter.PlatformServices/Helpers/StaThreadHelper.cs | Introduces a reusable STA-thread dispatch helper used by adapter execution paths. |
| src/Adapter/MSTestAdapter.PlatformServices/Execution/TestClassInfo.Initializer.cs | Uses the helper for ClassInitialize STA dispatch, removing duplicated thread/join logic. |
| src/Adapter/MSTestAdapter.PlatformServices/Execution/TestClassInfo.Cleanup.cs | Uses the helper for ClassCleanup STA dispatch, removing duplicated thread/join logic. |
Copilot's findings
- Files reviewed: 3/3 changed files
- Comments generated: 2
Comment on lines
+46
to
+50
| PlatformServiceProvider.Instance.AdapterTraceLogger.Error( | ||
| $"Failed to join STA thread '{threadName}': {ex}"); | ||
| } | ||
|
|
||
| return defaultResult; |
Comment on lines
+175
to
+179
| return await StaThreadHelper.RunOnStaThreadIfNeededAsync( | ||
| needsSta: ClassAttribute is STATestClassAttribute, | ||
| action: DoRunAsync, | ||
| threadName: "MSTest STATestClass ClassInitialize", | ||
| defaultResult: initializeErrorResult).ConfigureAwait(false); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The same ~40-line STA thread dispatch block (check → create named thread → set STA → start → join → warn on non-Windows) was copy-pasted in
TestClassInfo.Initializer.csandTestClassInfo.Cleanup.cs, making any bug fix or behavioral change require parallel edits.Changes
New
StaThreadHelper.RunOnStaThreadIfNeededAsync<TResult>inMSTestAdapter.PlatformServices/Helpers/— generic helper encapsulating the full STA dispatch pattern:needsSta && Windows && currentThread != STAThreadStateException | ThreadInterruptedExceptionfromJoin()(the only types documented to be thrown), logs with thread name context, returnsdefaultResultawaitotherwiseTestClassInfo.Initializer.csandTestClassInfo.Cleanup.cs— ~80 lines of duplicated dispatch replaced with 4–5 line call sites each:TestExecutionManager.DefaultFactoryAsyncandMSTestExecutor.RunTestsFromRightContextAsyncare out of scope — both use structurally different mechanisms (TCS-based exception bridging andTask.Run(Join, cancellationToken)respectively) that don't fit this abstraction.