-
Notifications
You must be signed in to change notification settings - Fork 357
Wait for testhost stderr to drain before reading its crash output #16128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jakub Jareš (nohwnd)
merged 1 commit into
microsoft:main
from
nohwnd:fix-testhost-crash-stderr-drain
Jun 17, 2026
+153
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Threading; | ||
|
|
||
| using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions; | ||
|
|
||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Tests for <see cref="ProcessHelper.WaitForErrorStreamToDrain"/>, the bounded wait that lets the process | ||
| /// exit callback observe the complete standard error output of a crashed test host. Without it, the exit | ||
| /// callback could read the asynchronously-collected stderr before all ErrorDataReceived callbacks had run, | ||
| /// dropping a crash callstack such as "Stack overflow." (the cause of the flaky | ||
| /// RunTestsShouldThrowOnStackOverflowException test). | ||
| /// </summary> | ||
| [TestClass] | ||
| public class ProcessHelperTests | ||
| { | ||
| private const int BudgetMs = 500; | ||
|
|
||
| [TestMethod] | ||
| public void WaitForErrorStreamToDrainShouldReturnOnceTheErrorStreamCloses() | ||
| { | ||
| using var errorStreamClosed = new ManualResetEventSlim(initialState: false); | ||
|
|
||
| // The stream reaches EOF a little later, mimicking a slow ErrorDataReceived delivery. | ||
| var setter = new Thread(() => | ||
| { | ||
| Thread.Sleep(150); | ||
| errorStreamClosed.Set(); | ||
| }) | ||
| { IsBackground = true }; | ||
|
|
||
| var stopwatch = Stopwatch.StartNew(); | ||
| setter.Start(); | ||
| ProcessHelper.WaitForErrorStreamToDrain(errorStreamClosed, budgetMilliseconds: 5000, elapsedMilliseconds: 0); | ||
| stopwatch.Stop(); | ||
| setter.Join(); | ||
|
|
||
| Assert.IsTrue(errorStreamClosed.IsSet, "The method must wait until the error stream is drained."); | ||
| Assert.IsLessThan( | ||
| 3000L, | ||
| stopwatch.ElapsedMilliseconds, | ||
| $"The method should return shortly after the stream closes, not at the budget timeout (took {stopwatch.ElapsedMilliseconds} ms)."); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WaitForErrorStreamToDrainShouldBeBoundedWhenTheErrorStreamNeverCloses() | ||
| { | ||
| // Models a grandchild process keeping the pipe open: EOF never arrives. | ||
| using var errorStreamClosed = new ManualResetEventSlim(initialState: false); | ||
|
|
||
| var stopwatch = Stopwatch.StartNew(); | ||
| ProcessHelper.WaitForErrorStreamToDrain(errorStreamClosed, BudgetMs, elapsedMilliseconds: 0); | ||
| stopwatch.Stop(); | ||
|
|
||
| Assert.IsFalse(errorStreamClosed.IsSet, "Precondition: the stream never closes in this test."); | ||
| Assert.IsGreaterThanOrEqualTo( | ||
| 150L, | ||
| stopwatch.ElapsedMilliseconds, | ||
| $"The method should wait roughly the budget for the stream (waited only {stopwatch.ElapsedMilliseconds} ms)."); | ||
| Assert.IsLessThan( | ||
| 5000L, | ||
| stopwatch.ElapsedMilliseconds, | ||
| $"The wait must be bounded so it cannot hang (took {stopwatch.ElapsedMilliseconds} ms)."); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WaitForErrorStreamToDrainShouldNotWaitWhenTheBudgetIsAlreadyExhausted() | ||
| { | ||
| // The exit wait above already consumed the whole budget (e.g. a slow grandchild), so there is no | ||
| // time left to wait for stderr - we must not add any latency on top. | ||
| using var errorStreamClosed = new ManualResetEventSlim(initialState: false); | ||
|
|
||
| var stopwatch = Stopwatch.StartNew(); | ||
| ProcessHelper.WaitForErrorStreamToDrain(errorStreamClosed, BudgetMs, elapsedMilliseconds: BudgetMs + 100); | ||
| stopwatch.Stop(); | ||
|
|
||
| Assert.IsFalse(errorStreamClosed.IsSet); | ||
| Assert.IsLessThan( | ||
| 250L, | ||
| stopwatch.ElapsedMilliseconds, | ||
| $"With the budget exhausted the method must return immediately (took {stopwatch.ElapsedMilliseconds} ms)."); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WaitForErrorStreamToDrainShouldReturnImmediatelyWhenThereIsNoErrorStream() | ||
| { | ||
| var stopwatch = Stopwatch.StartNew(); | ||
| ProcessHelper.WaitForErrorStreamToDrain(errorStreamClosed: null, BudgetMs, elapsedMilliseconds: 0); | ||
| stopwatch.Stop(); | ||
|
|
||
| Assert.IsLessThan( | ||
| 250L, | ||
| stopwatch.ElapsedMilliseconds, | ||
| $"With no redirected error stream the method must be a no-op (took {stopwatch.ElapsedMilliseconds} ms)."); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.