Prevent stdio connection hang for missing server path. by TimChild · Pull Request #401 · modelcontextprotocol/python-sdk · GitHub
Skip to content

Prevent stdio connection hang for missing server path. - #401

Merged
ihrpr merged 9 commits into
modelcontextprotocol:mainfrom
TimChild:prevent-stdio-hang
May 28, 2025
Merged

ihrpr merged 9 commits into
modelcontextprotocol:mainfrom
TimChild:prevent-stdio-hang

Conversation

@TimChild

Copy link
Copy Markdown
Contributor
  • Ensure that read/write streams are always closed (even if it is the process that ends first)
  • Check whether the stdio process ended prematurely
  • Raise an error if that was the case

Motivation and Context

The stdio connection was hanging indefinitely for a bad server file path (e.g. command uv, args run non-existing-file.py).
I think because the read/write streams were not being closed properly in that case.

This makes it so that errors due to the underlying mcp server stopping early raises a ProcessTerminatedEarlyError.

How Has This Been Tested?

Added a test (including catching if it does hang for more than 1s)

Breaking Changes

Should not be a breaking change.

Types of changes

  • [ x ] 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

  • [ x ] I have read the MCP Documentation
  • [ x ] My code follows the repository's style guidelines
  • [ x ] New and existing tests pass locally
  • [ x ] I have added appropriate error handling
  • [ x ] I have added or updated documentation as needed -- Did not update docs (but error is self-explanatory)

Additional context

@TimChild
TimChild force-pushed the prevent-stdio-hang branch from 6a85c7f to 90f224d Compare April 7, 2025 18:59
@ihrpr ihrpr added this to the r-05-25 milestone Apr 29, 2025
ihrpr
ihrpr previously requested changes May 27, 2025

@ihrpr ihrpr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! Sorry for the delay in reviews.

This PR identified an issue where the SDK is not handling missing server paths. I'd suggest a slightly different approach for a solution though. Should we implement error handling for the server path instead?

@TimChild

Copy link
Copy Markdown
Contributor Author

@ihrpr

ihrpr commented May 28, 2025

Copy link
Copy Markdown
Contributor

@ihrpr ihrpr modified the milestones: r-05-25, stdio shutdown May 28, 2025
@bhosmer-ant
bhosmer-ant self-requested a review May 28, 2025 21:31

@bhosmer-ant bhosmer-ant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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.

3 participants