Skip to content

Fix log accumulation across DynamicData test invocations by @Evangelink in #7925 (backport to rel/4.2)#7931

Merged
Evangelink merged 3 commits intomicrosoft:rel/4.2from
nohwnd-bot:backport/7925/to/rel/4.2
Apr 29, 2026
Merged

Fix log accumulation across DynamicData test invocations by @Evangelink in #7925 (backport to rel/4.2)#7931
Evangelink merged 3 commits intomicrosoft:rel/4.2from
nohwnd-bot:backport/7925/to/rel/4.2

Conversation

@nohwnd-bot
Copy link
Copy Markdown

Backport of #7925 to rel/4.2

/cc @Evangelink

GetOut(), GetErr(), and GetTrace() on TestContextImplementation returned
accumulated buffer contents but never cleared them. When DynamicData tests
reuse the same TestContext across multiple invocations, each subsequent
result contained all logs from all previous invocations, causing exponential
memory growth (>20 GB for simple projects).

Replace these with GetAndClearOutput(), GetAndClearError(), and
GetAndClearTrace() that clear the StringBuilder after extracting the value,
matching the existing GetAndClearDiagnosticMessages() pattern.

Fixes microsoft#7846
Address review feedback: ToString() + Clear() as two separate synchronized
calls could lose appended text if another thread writes between them. Add
a single synchronized GetAndClear() method to SynchronizedStringBuilder
that performs both operations under one lock.
- Add focused GetAndClear{Output,Error,Trace} unit tests in
  TestContextImplementationTests that verify the buffer is cleared after
  the first call (returns empty on second call).
- Add GetAndClearTrace() call to WritesFromBackgroundThreadShouldNotThrow
  to complete thread-safety coverage for all three buffer types.
- Consolidate regression test comments: single block comment noting that
  TestClassInfo and UnitTestRunner call sites use the same GetAndClear*
  methods tested in isolation.
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.

  • I strongly suggest dogfooding this internally before backporting.
  • I don't know if @nohwnd is currently looking at a different approach that should be considered instead.

@Evangelink
Copy link
Copy Markdown
Member

Validated the memory reduction. I wasn't able to reproduce the concurrency issue on the texts with the provided example.

@Evangelink Evangelink enabled auto-merge (squash) April 29, 2026 16:13
@Evangelink Evangelink merged commit 5212002 into microsoft:rel/4.2 Apr 29, 2026
18 checks passed
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.

3 participants