fix(client): wait for the whole server process tree to exit on stdio dispose - #1895
Open
iamAdarshh wants to merge 1 commit into
Open
iamAdarshh wants to merge 1 commit into
iamAdarshh wants to merge 1 commit into
Conversation
…dispose KillTree killed the entire process tree but only waited on the root process. On Windows the root is the cmd.exe /c wrapper, so DisposeAsync could return while the actual server (and conhost.exe) were still terminating and holding the working directory and open files. Snapshot the root's descendants before killing the tree, then wait on each of them within the same ShutdownTimeout. Descendants are discovered with a Toolhelp32 snapshot on Windows, /proc on Linux and proc_listchildpids on macOS. Start times guard against stale parent IDs from reused PIDs. Fixes part 1 of modelcontextprotocol#1894.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Addresses problem 1 of #1894. Problem 2 (requests sent during dispose never completing) is left for a separate PR because it touches session disposal ordering.
Problem
ProcessHelper.KillTreekills the entire process tree, then waits only on the root process. On Windows the root is thecmd.exe /cwrapper, soDisposeAsynccan return while the real server and itsconhost.exeare still terminating. They still hold the working directory and open files, so deleting or reusing those right after dispose fails with "being used by another process". The netstandard2.0taskkill /T /Fpath has the same gap.Fix
ShutdownTimeoutbudget, so the worst-case dispose time doesn't grow.Process.Kill(entireProcessTree: true)finds them:CreateToolhelp32Snapshot/Process32FirstW/Process32NextWsnapshot. This works on netstandard2.0 / .NET Framework too./proc/*/stat.proc_listchildpidsfrom libproc.AllowUnsafeBlocksis now enabled for every Core target, not just netstandard2.0, because thePROCESSENTRY32Wstruct needs afixedbuffer.The diff for
ProcessHelper.csis easier to read with whitespace hidden: the existing kill code is only re-indented inside a newtry/finallythat disposes the snapshottedProcessobjects.Using a job object with
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, as the issue suggests, would be more robust, and it could also replace thecmd.exewrapper (#1751). It's a bigger change, though, so I've left it as a possible follow-up.Testing
StdioClientTransportTests.DisposeAsync_WaitsForDescendantProcessesToExit. The server starts a long-running descendant (powershellunder thecmd.exewrapper on Windows,sleepundershon Unix) that writes its PID to a file. The test disposes the session and asserts that the descendant has already exited.ModelContextProtocol.Testsrun on net10.0 (macOS): 2,395 total, 0 failed, 3 skipped.sh→sleep,sh→sleep,sleep):KillTree.dotnet/sdk:10.0container): same result.