Skip to content

Prevent stdio connection hang for missing server path. #401

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

Merged
merged 9 commits into from
May 28, 2025

Conversation

TimChild
Copy link
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
Copy link
Contributor

@ihrpr ihrpr left a comment

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
Contributor Author

No problem!
It's been a while since I thought about this. Definitely checking the path first is an easier and more efficient way to catch the basic problem of a missing server file, but I don't remember if I was only using that as the simplest example of a faulty server.

I'm trying to remember if I had issues when I was developing an mcp server, and had some bug in the connection procedure or something and then that was causing the client to hang indefinitely.

For now, it probably makes sense to do a simpler check that the server file as you suggest, and then worry about more complicated cases if that is still an issue.

About to fly right now, and won't have a chance to come back to this for a little while. I'm happy for you to make any changes you want, otherwise I'll hopefully get to it in a few weeks.

@ihrpr
Copy link
Contributor

ihrpr commented May 28, 2025

Thank you @TimChild, I think #333 might actually resolve this as well, but we need to use the test you have here. I'll take a look!

@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
Copy link
Contributor

@bhosmer-ant bhosmer-ant left a comment

Choose a reason for hiding this comment

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

LGTM!

@ihrpr ihrpr merged commit f5dd324 into modelcontextprotocol:main May 28, 2025
19 of 20 checks passed
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