Fix process crash when testing Stderr (#1449)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Eric StJohn 2026-04-06 11:29:00 -07:00 • committed by GitHub
commit 2e7209c35a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 254 additions and 148 deletions

View file

@ -82,24 +82,29 @@ public class ClientConformanceTests
var process = new Process { StartInfo = startInfo };
process.OutputDataReceived += (sender, e) =>
// Protect callbacks with try/catch to prevent ITestOutputHelper from
// throwing on a background thread if events arrive after the test completes.
DataReceivedEventHandler outputHandler = (sender, e) =>
{
if (e.Data != null)
{
_output.WriteLine(e.Data);
try { _output.WriteLine(e.Data); } catch { }
outputBuilder.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
DataReceivedEventHandler errorHandler = (sender, e) =>
{
if (e.Data != null)
{
_output.WriteLine(e.Data);
try { _output.WriteLine(e.Data); } catch { }
errorBuilder.AppendLine(e.Data);
}
};
process.OutputDataReceived += outputHandler;
process.ErrorDataReceived += errorHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
@ -112,6 +117,8 @@ public class ClientConformanceTests
catch (OperationCanceledException)
{
process.Kill(entireProcessTree: true);
process.OutputDataReceived -= outputHandler;
process.ErrorDataReceived -= errorHandler;
return (
Success: false,
Output: outputBuilder.ToString(),
@ -119,6 +126,9 @@ public class ClientConformanceTests
);
}
process.OutputDataReceived -= outputHandler;
process.ErrorDataReceived -= errorHandler;
var output = outputBuilder.ToString();
var error = errorBuilder.ToString();
var success = process.ExitCode == 0 || HasOnlyWarnings(output, error);

View file

@ -136,24 +136,29 @@ public class ServerConformanceTests(ConformanceServerFixture fixture, ITestOutpu
var process = new Process { StartInfo = startInfo };
process.OutputDataReceived += (sender, e) =>
// Protect callbacks with try/catch to prevent ITestOutputHelper from
// throwing on a background thread if events arrive after the test completes.
DataReceivedEventHandler outputHandler = (sender, e) =>
{
if (e.Data != null)
{
output.WriteLine(e.Data);
try { output.WriteLine(e.Data); } catch { }
outputBuilder.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
DataReceivedEventHandler errorHandler = (sender, e) =>
{
if (e.Data != null)
{
output.WriteLine(e.Data);
try { output.WriteLine(e.Data); } catch { }
errorBuilder.AppendLine(e.Data);
}
};
process.OutputDataReceived += outputHandler;
process.ErrorDataReceived += errorHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
@ -166,6 +171,8 @@ public class ServerConformanceTests(ConformanceServerFixture fixture, ITestOutpu
catch (OperationCanceledException)
{
process.Kill(entireProcessTree: true);
process.OutputDataReceived -= outputHandler;
process.ErrorDataReceived -= errorHandler;
return (
Success: false,
Output: outputBuilder.ToString(),
@ -173,6 +180,9 @@ public class ServerConformanceTests(ConformanceServerFixture fixture, ITestOutpu
);
}
process.OutputDataReceived -= outputHandler;
process.ErrorDataReceived -= errorHandler;
return (
Success: process.ExitCode == 0,
Output: outputBuilder.ToString(),