Skip to content

[dotnet test][MTP] Don't replay live output in the exit-code summary - #55550

Merged
Evangelink merged 8 commits into
mainfrom
dev/amauryleve/live-output-duplication-bug
Aug 3, 2026
Merged

Evangelink merged 8 commits into
mainfrom
dev/amauryleve/live-output-duplication-bug

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Fixes #55549

Problem

On the MTP dotnet test path, the live output feature added by #51615 streams the test application's standard output/error to the terminal as they are produced. When the run ends with a non-zero exit code, the pre-existing exit-code summary still replayed the same captured buffer indented under Standard output / Standard error, so every captured line was printed twice.

Any failing test yields exit code 2, so this affected essentially every failing run of a test app that writes to the console. It was reported against dotnet/aspnet-api-versioning.

Reproduced with a minimal xUnit v3 (xunit.v3.mtp-v2) project on 11.0.100-rc.1.26380.103:

Running tests from ...\Repro.dll (net11.0|x64)
CONSOLE-PASS-LINE            <-- live output
CONSOLE-FAIL-LINE            <-- live output
failed Tests.FailingWithOutput (167ms)
  ...
...\Repro.dll (net11.0|x64) failed with 1 error(s) [+1/x1/?0] (1s 172ms)
Exit code: 2
  Standard output: CONSOLE-PASS-LINE     <-- same lines a second time
  CONSOLE-FAIL-LINE

The same project on 10.0.302 prints only the trailing block, so this is a regression.

Fix

ProcessOutputCollector now reports whether a stream actually engaged live streaming alongside its captured tail (GetCapturedOutput() returning (string Output, bool WasStreamedLive), read atomically), and TestApplicationHandler.OnTestProcessExited suppresses the terminal replay for streams that were already shown.

The summary is still printed when live streaming never engaged — older Microsoft.Testing.Platform versions that don't negotiate protocol 1.1.0, and processes that fail before the handshake, rely on it as the only place their output is surfaced. Exit code: N and the assembly identification are unchanged, and trace logging still receives the full captured text. Suppressing the summary rather than the live stream is the right way round: the summary copy is additionally lossy (truncated to 30 head + 10 tail lines by TruncateOutputForSummary, on top of the 200-line tail bound).

Two concurrency issues surfaced while making the live stream the only copy the user sees, both fixed here:

  • A line racing with protocol negotiation could be dropped entirely. The reader threads sample the streaming state before taking the collector lock, so negotiation could enable streaming in between; AddLine then queued the line without writing it while the capture was already reported as streamed, so the summary suppressed it too. AddLine now decides from the collector's own state rather than the caller's sample.
  • Live output could be reordered at the negotiation boundary. The flush and a reader thread could interleave so a later line was printed ahead of the buffered lines preceding it. A dedicated ordering lock now spans "decide what to write, then write it". It is deliberately separate from the state lock so that reading the capture never waits on terminal I/O — RunAsync reads the capture after a bounded 5-second wait for the reader tasks, a timeout that exists precisely because a reader can be stuck.

Verification

Original xUnit v3 repro against the locally built SDK now prints each console line exactly once.

New tests:

  • ProcessOutputCollectorTests — buffering vs. streaming, flush-on-negotiation, the bounded tail being a subset of what was streamed, the stale-sample case, ordering at the negotiation boundary, and that reading the capture does not wait on an in-flight write.
  • TestApplicationHandlerTests — no replay when already streamed (completion, handshake-failure, and artifact post-processing paths), and still reported when not streamed.
  • GivenDotnetTestForwardsTestHostOutput.RunFailingTestProjectWritingToConsole_ShouldNotAlsoReplayTheOutputInTheExitCodeSummary — end-to-end, asserts each marker appears exactly once, with a new TestProjectWithFailingTestAndConsoleOutput asset.

Each new regression test was empirically confirmed to fail against the corresponding pre-fix behavior, not just to pass against the fix. ProcessOutputCollectorTests, TestApplicationHandlerTests, TerminalTestReporterTests and GivenDotnetTestForwardsTestHostOutput pass locally; leaving the full suite to CI.

Evangelink and others added 5 commits July 31, 2026 14:10
…mary

Live output streaming (added with protocol 1.1.0 negotiation) forwards the test
application's standard output and error to the terminal as they are produced,
but the pre-existing end-of-run summary kept replaying the same captured buffer
whenever the process exited with a non-zero exit code. Any failing test yields
exit code 2, so every captured line was printed twice: once unindented as live
output and once indented under "Standard output" / "Standard error".

ProcessOutputCollector now reports whether a stream actually engaged live
streaming alongside its captured tail, and TestApplicationHandler suppresses the
terminal replay for streams that were already shown. The capture is still passed
in full to trace logging, and it is still replayed when live streaming never
engaged - older Microsoft.Testing.Platform versions that don't negotiate
protocol 1.1.0, and processes that fail before the handshake, rely on that
summary as the only place their output is surfaced.

The collector also now writes while holding its lock. The negotiation flush and
the stdout reader thread could otherwise interleave so that a later line was
printed ahead of the buffered lines preceding it. That was masked before because
the summary provided a correctly ordered copy; with the replay suppressed the
live stream is the only copy the user sees.

Fixes #55549

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 3ebfc437-eb58-47e0-9bbc-9b8f212510f4
The stdout/stderr reader samples the live-output streaming state before it takes
the collector's lock, so the value can be stale when protocol negotiation
enables streaming in between. AddLine then queued the line without writing it,
while the capture was already reported as streamed - so the summary suppressed
it too and the line was dropped from the terminal entirely.

Once streaming is enabled it never turns back off, so the collector now writes
based on its own state rather than the caller's sample.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 3ebfc437-eb58-47e0-9bbc-9b8f212510f4
Reading the capture must not wait on terminal IO: RunAsync gives the reader
threads a bounded grace period after the test process exits and then reads the
capture anyway, precisely because a reader can still be stuck. Holding the state
lock across the write made that read block behind an in-flight write.

Live output ordering is now enforced by a dedicated ordering lock taken around
'decide what to write, then write it', while the state lock is held only for the
state update. GetCapturedOutput takes the state lock alone, so it never waits on
the terminal.

Also make the ordering test deterministic: it now waits for the reader thread to
signal that it is about to call AddLine before probing whether AddLine can
complete, instead of relying on it being scheduled within a fixed delay.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 3ebfc437-eb58-47e0-9bbc-9b8f212510f4
Reading the capture on the test thread meant a regression that put that read
behind the blocked write would deadlock only until the callback's own 30 second
timeout released it, and the test would then still pass - just far more slowly.
The read now runs on its own thread and the test asserts it completes while the
write is still blocked, which was verified to fail when the write is moved back
under the state lock.

Spawned threads are background so one left blocked by a regression cannot keep
the test host alive.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 3ebfc437-eb58-47e0-9bbc-9b8f212510f4
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 3ebfc437-eb58-47e0-9bbc-9b8f212510f4
Copilot AI review requested due to automatic review settings July 31, 2026 12:55
@Evangelink
Evangelink requested a review from a team as a code owner July 31, 2026 12:55
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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 regression in the Microsoft.Testing.Platform (MTP) dotnet test path where test-host stdout/stderr that was already streamed live to the terminal was later replayed again in the non-zero exit-code summary, resulting in duplicated output for failing runs.

Changes:

  • Extend ProcessOutputCollector to atomically report both the captured output tail and whether that stream was already streamed live, and harden streaming/flush ordering with an explicit write-order lock.
  • Update TestApplicationHandler.OnTestProcessExited to suppress summary replay for streams that were already shown live (while preserving trace logging of full captured output).
  • Add targeted unit and end-to-end regression tests plus a new failing MTP test asset to validate “exactly once” output behavior across relevant paths.
Show a summary per file
File Description
src/Cli/dotnet/Commands/Test/MTP/TestApplication.cs Uses the new (Output, WasStreamedLive) capture API and updates live-streaming flush/write ordering semantics in ProcessOutputCollector.
src/Cli/dotnet/Commands/Test/MTP/TestApplicationHandler.cs Suppresses exit-code summary output replay when that stream was already forwarded live.
test/dotnet.Tests/CommandTests/Test/ProcessOutputCollectorTests.cs New unit coverage for buffering/streaming transitions, stale streaming-state sampling, ordering at the negotiation boundary, and non-blocking capture reads.
test/dotnet.Tests/CommandTests/Test/TestApplicationHandlerTests.cs Regression coverage ensuring streamed output is not replayed via summary paths (completion, handshake-failure, artifact post-processing).
test/dotnet.Tests/CommandTests/Test/GivenDotnetTestForwardsTestHostOutput.cs Adds an E2E regression test asserting failing runs do not duplicate live-streamed output in the exit-code summary.
test/TestAssets/TestProjects/TestProjectWithFailingTestAndConsoleOutput/TestProjectWithFailingTestAndConsoleOutput.csproj New test asset project used by the E2E failing-run duplication test.
test/TestAssets/TestProjects/TestProjectWithFailingTestAndConsoleOutput/Program.cs MTP test app that prints unique stdout/stderr markers and reports a failing test node to induce non-zero exit behavior.
test/TestAssets/TestProjects/TestProjectWithFailingTestAndConsoleOutput/global.json Configures the test asset to use the MTP runner.

Copilot's findings

  • Files reviewed: 8/8 changed files
  • Comments generated: 0

@Evangelink
Evangelink enabled auto-merge July 31, 2026 14:11
Evangelink and others added 3 commits July 31, 2026 16:34
…ters

Addresses PR feedback: the caller already knows whether the output was streamed,
so it passes an empty string in that case rather than telling the handler to
blank it. ProcessOutputCollector.GetCapturedOutput() becomes GetOutputToReport(),
which returns empty once streaming engaged - and skips materializing the joined
string entirely in that case.

TestApplicationHandler is now untouched by this change: OnTestProcessExited keeps
its original three parameters.

The one consequence is that trace logging no longer records output that was
already streamed live, since a single value now feeds both the summary and the
trace. Keeping both would require passing the text and the streamed flag
separately, which is exactly the extra parameters this removes.

The handler tests move accordingly: asserting 'already streamed output is not
replayed' there would now be vacuous, so they assert what the handler still owns
- an empty payload renders no output block, a non-empty one still does, the
assembly stays identified, and artifact post-processing honours both.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 3ebfc437-eb58-47e0-9bbc-9b8f212510f4
The trace file (DOTNET_CLI_TEST_TRACEFILE) writes to disk only - there is no
console sink - so it is collected as a standalone artifact. Reporting nothing for
output that was streamed live therefore made it disappear from the trace as well,
which is exactly the diagnostic someone enabling it would be looking for.

RunAsync now records the full capture for the streams the handler will not log,
guarded on Logger.TraceEnabled so nothing is materialized in normal runs. The
handler keeps its original three parameters.

Also harden the end-to-end test: asserting the marker appears exactly once passes
against a host that never streams at all, so it would silently stop exercising the
regression. It now also asserts the surviving copy is the live one, which sits
alone on its line rather than folded into the indented summary line.

Verified end to end against both paths: with a protocol 1.0.0 host the trace keeps
the pre-existing 'Output Data:' entry, and with a 1.1.0+ host it gets
'Output Data (already streamed live):' while the terminal shows the output once.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 3ebfc437-eb58-47e0-9bbc-9b8f212510f4
@Evangelink
Evangelink merged commit 03487ce into main Aug 3, 2026
22 checks passed
@Evangelink
Evangelink deleted the dev/amauryleve/live-output-duplication-bug branch August 3, 2026 15:23
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 4, 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.

[dotnet test][MTP] Live output is duplicated by the exit-code summary's "Standard output" block

4 participants