Skip to content

Add regression test for issue #1027 #1069

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

Open
wants to merge 5 commits into
base: fweinberger/stream-cleanup-only-approach
Choose a base branch
from

Conversation

felixweinberger
Copy link
Contributor

@felixweinberger felixweinberger commented Jul 1, 2025

Motivation and Context

Issue #1027 reports that MCP server cleanup code (after yield in lifespan) is unreachable when the process is terminated. This PR adds tests that:

  1. Demonstrate the issue exists on all platforms (not just Windows)
  2. Verify that PR Unify process termination on POSIX & Windows (+ tests) #1044's approach of closing stdin first would fix the issue

How Has This Been Tested?

Added two tests in test_1027_win_unreachable_cleanup.py:

  • test_lifespan_cleanup_executed - Shows cleanup doesn't run with current termination (xfails)
  • test_stdin_close_triggers_cleanup - Shows cleanup works when stdin is closed first (passes)

Tested on both macOS and Windows.

When run with PR #1044's changes, both tests pass.

Breaking Changes

None. Tests only.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

felixweinberger and others added 4 commits July 1, 2025 19:40
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.

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 or perform lengthy cleanup operations.

Also adds regression tests for #559.

resolves #555

Co-authored-by: Cristian Pufu <[email protected]>
The MCP specification recommends closing stdin first to allow servers
to exit gracefully before resorting to signals. This approach gives
well-behaved servers the opportunity to detect stdin closure and
perform clean shutdown without forceful termination.

The shutdown sequence now follows a graceful escalation path: first
closing stdin and waiting 2 seconds for voluntary exit, then sending
SIGTERM if needed, and finally using SIGKILL as a last resort. This
minimizes the risk of data loss or corruption while ensuring cleanup
always completes.

This unified approach works consistently across all platforms and
improves compatibility with MCP servers that monitor stdin for
lifecycle management.

resolves #765

Co-authored-by: davenpi <[email protected]>
When terminating MCP servers, child processes were being orphaned
because only the parent process was killed. This caused resource
leaks and prevented proper cleanup, especially with tools like npx
that spawn child processes for the actual server implementation.

This was happening on both POSIX and Windows systems - however
because of implementation details, resolving this is non-trivial
and requires introducing psutil to introduce cross-platform
utilities for dealing with children and process trees.

This addresses critical issues where MCP servers using process
spawning tools would leave zombie processes running after client
shutdown.

resolves #850
resolves #729

Co-authored-by: jingx8885 <[email protected]>
Co-authored-by: Surya Prakash Susarla <[email protected]>
Refactor the MCP spec-compliant stdio shutdown sequence into a separate
_shutdown_process function to document that this is an MCP specific
shutdown sequence. Logic remains unchanged.

Co-authored-by: davenpi <[email protected]>
@felixweinberger felixweinberger changed the base branch from main to fweinberger/stream-cleanup-only-approach July 1, 2025 20:29
@felixweinberger felixweinberger force-pushed the fweinberger/issue-1027 branch from 0e4626a to c032dbc Compare July 1, 2025 20:30
@felixweinberger felixweinberger marked this pull request as ready for review July 1, 2025 20:35
@felixweinberger felixweinberger force-pushed the fweinberger/issue-1027 branch 3 times, most recently from 4fcba8b to 1224de0 Compare July 1, 2025 20:45
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
@felixweinberger felixweinberger force-pushed the fweinberger/issue-1027 branch from 1224de0 to 3c8eabe Compare July 1, 2025 20:48
@felixweinberger felixweinberger force-pushed the fweinberger/stream-cleanup-only-approach branch 11 times, most recently from 0205aa0 to 2be6f09 Compare July 4, 2025 17:13
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.

1 participant