-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Unify process termination on POSIX & Windows (+ tests) #1044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unify process termination on POSIX & Windows (+ tests) #1044
Conversation
9f88ea7
to
a27fcef
Compare
caa5628
to
1d495ca
Compare
9e8d74b
to
3f6c472
Compare
cbaee2e
to
e8954dd
Compare
e817dfb
to
7af9e65
Compare
Hi @Kludex, @agronholm thanks for taking a look! For context, we actually don't spawn windows processes with However, your comment made me realize that we should probably explicitly guard for that case and still use If this could somehow be fixed upstream in |
Any idea why |
The stdio cleanup was hanging indefinitely when processes ignored termination signals or took too long to exit. This caused the MCP client to freeze during shutdown, especially with servers that don't handle SIGTERM properly. This was already being handled on Windows, but not Unix systems. This Commit unifies the two approaches, removing special logic for windows process termination. The fix introduces a 2-second timeout for process termination. If a process doesn't exit gracefully within this window, it's forcefully killed. This ensures the client always completes cleanup in bounded time while still giving well-behaved servers a chance to exit cleanly. This resolves hanging issues reported when MCP servers ignore standard termination signals. resolves #555 Also adds regression tests for #559. Co-authored-by: Cristian Pufu <cristian.pufu@uipath.com>
This re-establishes behavior before #596 in the default case. - Attempt to use anyio's native open_process function on Windows - Fall back to subprocess.Popen only if NotImplementedError is raised - This improves compatibility with event loops that support async subprocesses - Extract fallback logic into separate function for clarity
7af9e65
to
c9b43bc
Compare
@agronholm Doing some research based on what @theailanguage raised in #596:
It seems that while the |
This test shows that MCP server cleanup code in lifespan doesn't run when the process is terminated, but does run when stdin is closed first (as implemented in PR #1044). The test includes: - Demonstration of current broken behavior (cleanup doesn't run) - Verification that stdin closure allows graceful shutdown - Windows-specific ResourceWarning handling - Detailed documentation of the issue and solution Github-Issue:#1027
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
left a few comments
- Add SIGTERM_IGNORING_PROCESS_TIMEOUT constant in tests to document timeout behavior - Add PROCESS_TERMINATION_TIMEOUT constant to replace magic number in stdio client - Restore deprecated terminate_windows_process function with original functionality to maintain backward compatibility for external users The deprecated function is marked using @deprecated decorator following the codebase convention, while preserving its original terminate-wait-kill behavior.
9fd111e
to
5771abc
Compare
This test shows that MCP server cleanup code in lifespan doesn't run when the process is terminated, but does run when stdin is closed first (as implemented in PR #1044). The test includes: - Demonstration of current broken behavior (cleanup doesn't run) - Verification that stdin closure allows graceful shutdown - Windows-specific ResourceWarning handling - Detailed documentation of the issue and solution Github-Issue:#1027
This test shows that MCP server cleanup code in lifespan doesn't run when the process is terminated, but does run when stdin is closed first (as implemented in PR #1044). The test includes: - Demonstration of current broken behavior (cleanup doesn't run) - Verification that stdin closure allows graceful shutdown - Windows-specific ResourceWarning handling - Detailed documentation of the issue and solution Github-Issue:#1027
This test shows that MCP server cleanup code in lifespan doesn't run when the process is terminated, but does run when stdin is closed first (as implemented in PR #1044). The test includes: - Demonstration of current broken behavior (cleanup doesn't run) - Verification that stdin closure allows graceful shutdown - Windows-specific ResourceWarning handling - Detailed documentation of the issue and solution Github-Issue:#1027
This test shows that MCP server cleanup code in lifespan doesn't run when the process is terminated, but does run when stdin is closed first (as implemented in PR #1044). The test includes: - Demonstration of current broken behavior (cleanup doesn't run) - Verification that stdin closure allows graceful shutdown - Windows-specific ResourceWarning handling - Detailed documentation of the issue and solution Github-Issue:#1027
* Initial refactor * draft concept for servers * remove unused * Remove ai generated content * fix navigation * header * clean up * updated * Initial refactor * Remove ai generated content * fix broken links * feat: create docs-v2 structure with diataxis framework - Create new docs-v2 directory with modern docs.json configuration - Implement 3-tab navigation: Overview, Documentation, Community - Structure Documentation section following diataxis framework: - Tutorials (learning-oriented) - Learn/Explanations (understanding-oriented) - How-to Guides (task-oriented) - Reference (information-oriented) - Add 44 stub files for team members to populate - Update package.json with docs-v2 scripts - Use modern docs.json format with proper tabs support 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * format * fix merge * fix * merge * fix * build new structure in existing docs folder * purge docs-v2 * update structure * create dummy structure * small rephrasing * change style to more technical * merge * simplify tool * format * structure * titles * add client concepts * apply suggested changes * format * apply suggested changes * Update architecture.mdx with comprehensive MCP overview Replace placeholder content with detailed documentation covering: - Core MCP concepts and scope - Participants (host, client, server) - Protocol layers (data and transport) - Lifecycle management, primitives, and notifications * Add comprehensive MCP protocol walkthrough to architecture docs Includes step-by-step examples of initialization, tool discovery, execution, and real-time notifications with detailed JSON-RPC message exchanges. Co-Authored-By: Assistant <noreply@anthropic.com> * Improvements * Improve MCP architecture documentation clarity and structure - Fix typos and grammar issues throughout the document - Add clearer explanations for MCP primitives and their use cases - Restructure content for better readability and flow - Enhance descriptions of client-exposed primitives with practical examples 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * MCP protocol -> MCP * Add SDK documentation page - Create comprehensive SDK documentation listing all official MCP SDKs - Include TypeScript, Python, Java, Kotlin, C#, and Ruby SDKs - Link to package registries and GitHub repositories - Credit maintainers for each SDK 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * fixup * Update docs/docs/learn/architecture.mdx Co-authored-by: Inna Harper <inna.hrpr@gmail.com> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update docs/docs/learn/architecture.mdx Co-authored-by: Jonathan Hefner <jonathan@hefner.pro> * Update architecture documentation - Clarify that reference implementations are specifically for servers - Simplify data/transport layer explanation by removing Info component - Update client features description for clarity 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * prettier * non code example * fix broken links * apply suggested changes * small fixed * clean up * move lern after Getting Started * Fix broken anchor links * Remove reference to nonexistent section While there is a "Transport Layer" section (which is linked in a prior bullet point), there is no section for an overview of the transport protocol. * getting started intro * Fix typos * Shorten introduction in architecture overview The automatically-generated table of contents will provide links to individual sections, so the introduction need not do so comprehensively. Instead, the introduction can just link to a few key sections. * fix slyles after merge * remove reference * community tab * use mcp * use remote mcp * use local mcp server * link old pages * update names for server and client * sdks page * add missing SDKs * remove discord - communications page will be used * initial landing page * remove get started from landing page * improve landing page * more changes to index * CI * CI * CI * CI * broken links * CI * fix oops * docs: improve architecture documentation clarity Enhance readability of MCP architecture docs by: - Simplifying client-server relationship explanation - Adding visual mermaid diagram to illustrate connections - Clarifying server vs client features descriptions - Converting Note block to inline text for better flow - Fixing SDK reference link 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * address theos feedback * server contepts feedback * client concepts feedback * sdks comments * intro feedback * remote server * user page feednacl * CI * suggestes changes from basil * format * documentation as a first tab * Remove protocol from specification tab * bring back schema reference * documentation page structure * fix broken links * reorganize documentation * fix broken SDKs link * move faq and remove how to * server and client examples * back to old structure in documents * remove mcp logo on landing page * format * remove video (will be added back soon) * fix AI-powered to AI-enable * Add inverted logo to overview page with two-column layout - Added logo2.jpg to the overview page - Created two-column layout with text on left, logo on right - Applied invert filter to flip logo colors from white-on-black to black-on-white - Added rounded corners to logo image 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Reorganize overview page with MCP logo sidebar layout Place "Connect your AI applications" content on left with MCP logo on right. Ensure consistent section widths across the page for better visual alignment. Add responsive design for mobile screens. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * remove logos * remove hiding of the overview --------- Co-authored-by: Jerome <jerome@anthropic.com> Co-authored-by: Felix Weinberger <fweinberger@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com> Co-authored-by: David Soria Parra <167242713+dsp-ant@users.noreply.github.com> Co-authored-by: David Soria Parra <davidsp@anthropic.com> Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
It appears that this was mistakenly left behind when the site was redesigned in modelcontextprotocol#1044. The `<Card>` components containing the link information for each SDK are defined in the `docs/docs/sdk.mdx` file: https://github.com/modelcontextprotocol/modelcontextprotocol/blob/0695a49/docs/docs/sdk.mdx So the `.mdx` files under the `docs/links/sdks` directory no longer seem necessary.
Motivation and Context
#555, #559 address important but different ways MCP servers can hang or fail to successfully terminate on Windows & POSIX systems.
This PR re-implements #555 by aligning process termination between Windows and Unix to make sure SIGTERM-ignoring processes don't inadvertently cause hangs.
How Has This Been Tested?
New regression tests added for #555 and #559. These were developed and tested on both POSIX and Windows systems.
Breaking Changes
None.
Types of changes
Checklist
Additional context