Skip to content

fix: kill process tree on StdioClientTransport.close()#2024

Open
JoannaaKL wants to merge 1 commit intomodelcontextprotocol:mainfrom
JoannaaKL:kill-process-tree
Open

fix: kill process tree on StdioClientTransport.close()#2024
JoannaaKL wants to merge 1 commit intomodelcontextprotocol:mainfrom
JoannaaKL:kill-process-tree

Conversation

@JoannaaKL
Copy link
Copy Markdown

Summary

Approach

This matches the dominant pattern in the Node.js ecosystem (used by execa, n8n, Mastra):

  • Unix: detached: true + process.kill(-pid, signal) — atomic, single syscall, no race conditions
  • Windows: taskkill /T /F /PID — the only viable tree-kill mechanism on Windows; detached is skipped to avoid opening a console window

Test coverage

Added 7 new tests:

  • Kill single grandchild process
  • Kill multiple (3) grandchildren
  • Kill 3-level deep process tree
  • onclose callback fires correctly with tree kill
  • close() on already-exited process doesn't throw
  • close() called twice doesn't throw
  • close() called without start() doesn't throw

@JoannaaKL JoannaaKL requested a review from a team as a code owner May 6, 2026 15:51
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 6, 2026

⚠️ No Changeset found

Latest commit: 8d89c33

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 6, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2024

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2024

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2024

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2024

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2024

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2024

commit: 8d89c33

close() only signaled the direct child PID, leaving descendants
(e.g. from npx/uvx wrappers) running as orphans.

- Spawn with detached: true on Unix so the child leads its own
  process group
- Use process.kill(-pid) to kill the entire group on Unix
- Use taskkill /T /F on Windows for tree kill
- Fall back to direct ChildProcess.kill() if group kill fails

Closes modelcontextprotocol#2023
@JoannaaKL JoannaaKL force-pushed the kill-process-tree branch from e2897ec to 8d89c33 Compare May 6, 2026 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StdioClientTransport.close() does not kill the process tree, leaving orphan processes

1 participant