Skip to content

Conversation

@Youssef1313
Copy link
Member

Fixes #5729

@Evangelink Evangelink enabled auto-merge January 12, 2026 21:02
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 adds a UseSTASynchronizationContext property to STATestMethodAttribute to enable preserving the STA thread context for async continuations. When enabled, the attribute creates a custom SynchronizationContext that ensures async continuations run on the same STA thread, which is useful for testing UI components that require STA threading.

Changes:

  • Introduced SingleThreadedSTASynchronizationContext class that manages a dedicated STA thread and routes all operations to it
  • Added UseSTASynchronizationContext property to STATestMethodAttribute with default value of false
  • Modified STATestMethodAttribute.ExecuteAsync to conditionally use the new synchronization context
  • Added tests to verify both default behavior (no sync context) and new behavior (with sync context)

Reviewed changes

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

File Description
src/TestFramework/TestFramework/Attributes/TestMethod/SingleThreadedSTASynchronizationContext.cs New internal class that implements a custom SynchronizationContext with a dedicated STA thread for routing operations
src/TestFramework/TestFramework/Attributes/TestMethod/STATestMethodAttribute.cs Added UseSTASynchronizationContext property and modified ExecuteAsync to optionally use the new sync context
src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt Added public API entries for the new property getter and setter
test/UnitTests/MSTest.SelfRealExamples.UnitTests/STATestMethodSyncContext.cs Added integration tests verifying both default and new behavior

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

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

SetSynchronizationContext(this);
foreach (Action callback in _queue.GetConsumingEnumerable())
{
callback();
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

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

Exceptions thrown by the callback in the thread loop are not handled and will terminate the background thread. This could leave queued actions unexecuted and cause hangs in Send calls waiting for done.Set(). Consider wrapping the callback invocation in a try-catch block to handle and potentially log exceptions.

Suggested change
callback();
try
{
callback();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Exception in {nameof(SingleThreadedSTASynchronizationContext)} callback: {ex}");
}

Copilot uses AI. Check for mistakes.
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.

STA-Thread lost after async operation

3 participants