…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.
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.KillTree kills the entire process tree, then waits only on the root process. On Windows the root is the cmd.exe /c wrapper, so DisposeAsync can return while the real server and its conhost.exe are 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.0 taskkill /T /F path has the same gap.
Fix
The diff for ProcessHelper.cs is easier to read with whitespace hidden: the existing kill code is only re-indented inside a new try/finally that disposes the snapshotted Process objects.
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 the cmd.exe wrapper (#1751). It's a bigger change, though, so I've left it as a possible follow-up.
Testing