From 3caac28a76b0780a81f0fc8ec2d36a995bd0637b Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sun, 15 Feb 2026 09:55:24 -0500 Subject: [PATCH] Fix flaky stderr test: call WaitForExit() after WaitForExitAsync() to flush ErrorDataReceived events (#1278) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- .../Client/StdioClientSessionTransport.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ModelContextProtocol.Core/Client/StdioClientSessionTransport.cs b/src/ModelContextProtocol.Core/Client/StdioClientSessionTransport.cs index 78b0d6f..f8b1074 100644 --- a/src/ModelContextProtocol.Core/Client/StdioClientSessionTransport.cs +++ b/src/ModelContextProtocol.Core/Client/StdioClientSessionTransport.cs @@ -72,11 +72,13 @@ internal sealed class StdioClientSessionTransport( try { // The process has exited, but we still need to ensure stderr has been flushed. + // WaitForExitAsync only waits for exit; it does not guarantee that all + // ErrorDataReceived events have been dispatched. The synchronous WaitForExit() + // (no arguments) does ensure that, so call it after WaitForExitAsync completes. #if NET await _process.WaitForExitAsync(cancellationToken).ConfigureAwait(false); -#else - _process.WaitForExit(); #endif + _process.WaitForExit(); } catch { }