Skip to content

Commit 50263cf

Browse files
Copilotstephentoub
andcommitted
Fix race condition in DisposeProcess that could skip flushing stderr events
When GetUnexpectedExitExceptionAsync sees HasExited as false and returns null, DisposeProcess is called next. By that time the process may have exited, causing it to skip KillTree and call Dispose() without first calling WaitForExit() to flush ErrorDataReceived callbacks. Add a WaitForExit() call for already-exited processes to ensure all redirected output events are dispatched before disposal. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 796d7d4 commit 50263cf

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

src/ModelContextProtocol.Core/Client/StdioClientTransport.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ internal static void DisposeProcess(
227227
// and Node.js does not kill its children when it exits properly.
228228
process.KillTree(shutdownTimeout);
229229
}
230+
else if (HasExited(process))
231+
{
232+
// The process has already exited. Call WaitForExit() (no arguments)
233+
// to ensure all redirected stderr/stdout events have been dispatched
234+
// before disposing.
235+
process.WaitForExit();
236+
}
230237
}
231238
finally
232239
{

0 commit comments

Comments
 (0)