Repository navigation
Fix flaky Nuget_reference_compat test: make TestContextOutputHelper thread-safe - #54985
Merged
Evangelink merged 1 commit intoJun 26, 2026
Merged
Conversation
…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]>
Contributor
There was a problem hiding this comment.
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
StringBuilderandTestContextwrite operations. - Consolidate
Outputto delegate toToString(), ensuringOutputis also synchronized.
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
approved these changes
Jun 26, 2026
Evangelink
deleted the
michaelsimons/fix-nuget-reference-compat-flaky-test
branch
June 26, 2026 10:45
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.
Summary
Fixes #54960
TestContextOutputHelper(introduced in #54769) is not thread-safe, but it is used concurrently fromParallel.ForEachinNuget_reference_compatwhere multiple threads share the sameLoginstance and callExecute()→TestContextOutputHelper.WriteLine()simultaneously.Neither
StringBuildernorTestContext.WriteLineare thread-safe, so the concurrent writes intermittently race on the internal buffer, producing:Fix
Added a
lockaround allStringBuilderandTestContextoperations inTestContextOutputHelper. Also consolidated the duplicateOutputproperty to delegate toToString().Impact
Test infrastructure only — no product code changes.