Skip to content

Commit 891c34c

Browse files
Copilotstephentoub
andcommitted
Only call WaitForExit() when process exited on its own, not after KillTree
The previous fix called WaitForExit() unconditionally or guarded only by HasExited, which could hang on Windows when KillTree left child processes holding output handles. Now WaitForExit() is only called when the process exited before DisposeProcess was called (!processRunning), meaning KillTree was not invoked. In that case, no child processes hold handles and WaitForExit() completes quickly. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 1826e6b commit 891c34c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/ModelContextProtocol.Core/Client/StdioClientTransport.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,12 @@ internal static void DisposeProcess(
230230

231231
// Ensure all redirected stderr/stdout events have been dispatched
232232
// before disposing. Only the no-arg WaitForExit() guarantees this;
233-
// WaitForExit(int) (used by KillTree) does not. Guard with HasExited
234-
// to avoid hanging indefinitely if the process couldn't be killed.
235-
if (HasExited(process))
233+
// WaitForExit(int) (as used by KillTree) does not.
234+
// This should not hang: either the process already exited on its own
235+
// (no child processes holding handles), or KillTree killed the entire
236+
// process tree. If it does take too long, the test infrastructure's
237+
// own timeout will catch it.
238+
if (!processRunning && HasExited(process))
236239
{
237240
process.WaitForExit();
238241
}

0 commit comments

Comments
 (0)