Skip to content

Fix issue #428: ClientSession.initialize gets stuck if the MCP server process exits #434

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 1 commit into
base: main
Choose a base branch
from

Conversation

AshwanthramKL
Copy link

This PR fixes an issue where ClientSession.initialize gets stuck indefinitely if the MCP server process exits unexpectedly.

Motivation and Context

When using stdio_client to connect to an MCP server, if the server process exits unexpectedly (e.g., due to missing API keys or configuration), the client hangs indefinitely without raising an appropriate exception. This makes for a poor developer experience and can be frustrating to debug.

How Has This Been Tested?

Added a test case that verifies the fix works correctly by creating a server that exits during initialization and checking that the appropriate exception is raised. The test ensures that the client properly detects when the server process exits and raises a ProcessTerminatedEarlyError instead of hanging indefinitely.

Breaking Changes

None. This change only improves error handling and doesn't modify any existing APIs.

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

The solution adds a process monitoring task that detects when the server process exits unexpectedly and raises a ProcessTerminatedEarlyError with a descriptive message. It also ensures proper cleanup of resources to prevent leaks.

Specifically:

  • Added a ProcessTerminatedEarlyError exception class
  • Added a process monitoring task that detects when the process exits unexpectedly
  • Properly closed all streams to prevent resource leaks
  • Added error handling for the case where the process has already exited
  • Made sure we don't raise an error when we explicitly terminate the process

Fixes #428

@AshwanthramKL AshwanthramKL force-pushed the fix/428-stdio-client-hang branch from b3ac56d to 2a395cd Compare April 5, 2025 08:57
@AshwanthramKL AshwanthramKL force-pushed the fix/428-stdio-client-hang branch from 2a395cd to 728ec85 Compare April 5, 2025 09:01
@ihrpr ihrpr added this to the r-05-25 milestone May 13, 2025
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.

ClientSession.initialize gets stuck if the MCP server process exits
2 participants