Skip to content

Fix flaky Nuget_reference_compat test: make TestContextOutputHelper thread-safe - #54985

Merged
Evangelink merged 1 commit into
mainfrom
michaelsimons/fix-nuget-reference-compat-flaky-test
Jun 26, 2026
Merged

Evangelink merged 1 commit into
mainfrom
michaelsimons/fix-nuget-reference-compat-flaky-test

Conversation

@MichaelSimons

Copy link
Copy Markdown
Member

Summary

Fixes #54960

TestContextOutputHelper (introduced in #54769) is not thread-safe, but it is used concurrently from Parallel.ForEach in Nuget_reference_compat where multiple threads share the same Log instance and call Execute() → TestContextOutputHelper.WriteLine() simultaneously.

Neither StringBuilder nor TestContext.WriteLine are thread-safe, so the concurrent writes intermittently race on the internal buffer, producing:

System.AggregateException: One or more errors occurred.
  (Destination is too short. (Parameter 'destination'))
  ---> System.ArgumentException: Destination is too short. (Parameter 'destination')
    at System.Text.StringBuilder.AppendWithExpansion(...)
    at Microsoft.NET.TestFramework.TestContextOutputHelper.WriteLine(...)

Fix

Added a lock around all StringBuilder and TestContext operations in TestContextOutputHelper. Also consolidated the duplicate Output property to delegate to ToString().

Impact

Test infrastructure only — no product code changes.

…elper

The test uses Parallel.ForEach which shares a single Log (TestContextOutputHelper)
instance across multiple threads. Neither StringBuilder nor TestContext.WriteLine
are thread-safe, causing intermittent 'Destination is too short' ArgumentException
when concurrent calls race on the internal buffer.

Fix by adding a lock around all StringBuilder and TestContext operations.
Also consolidate Output property to delegate to ToString() since they did
the same thing.

Fixes #54960

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 25, 2026 19:34

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 fixes a flaky MSTest infrastructure failure by making TestContextOutputHelper safe to use from multiple threads (preventing concurrent StringBuilder mutations and concurrent TestContext writes from racing).

Changes:

  • Add a private lock and synchronize all StringBuilder and TestContext write operations.
  • Consolidate Output to delegate to ToString(), ensuring Output is also synchronized.

@MichaelSimons
MichaelSimons requested a review from Evangelink June 25, 2026 19:37
@Evangelink

Copy link
Copy Markdown
Member

Given you are doing extra work, it's probably not worth for me to try to make the part of MSTest TestContext thread-safe but in the future if you find any issue, feel free to ping or report issue to MSTest instead of "working around it".

@Evangelink
Evangelink merged commit aeb1f90 into main Jun 26, 2026
27 checks passed
@Evangelink
Evangelink deleted the michaelsimons/fix-nuget-reference-compat-flaky-test branch June 26, 2026 10:45
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jun 29, 2026
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: Nuget_reference_compat fails with ArgumentException "Destination is too short"

3 participants