Repository navigation
[dotnet test][MTP] Don't replay live output in the exit-code summary - #55550
Merged
Merged
Conversation
…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
|
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. |
Contributor
There was a problem hiding this comment.
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
ProcessOutputCollectorto 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.OnTestProcessExitedto 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
enabled auto-merge
July 31, 2026 14:11
Youssef1313
reviewed
Jul 31, 2026
…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
0101
approved these changes
Aug 3, 2026
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.
Fixes #55549
Problem
On the MTP
dotnet testpath, 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 underStandard 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 on11.0.100-rc.1.26380.103:The same project on
10.0.302prints only the trailing block, so this is a regression.Fix
ProcessOutputCollectornow reports whether a stream actually engaged live streaming alongside its captured tail (GetCapturedOutput()returning(string Output, bool WasStreamedLive), read atomically), andTestApplicationHandler.OnTestProcessExitedsuppresses 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: Nand 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 byTruncateOutputForSummary, 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:
AddLinethen queued the line without writing it while the capture was already reported as streamed, so the summary suppressed it too.AddLinenow decides from the collector's own state rather than the caller's sample.RunAsyncreads 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 newTestProjectWithFailingTestAndConsoleOutputasset.Each new regression test was empirically confirmed to fail against the corresponding pre-fix behavior, not just to pass against the fix.
ProcessOutputCollectorTests,TestApplicationHandlerTests,TerminalTestReporterTestsandGivenDotnetTestForwardsTestHostOutputpass locally; leaving the full suite to CI.