python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

Align stdio shutdown sequence with spec

Open davenpi opened this issue 7 months ago • 2 comments

Align the stdio shutdown sequence with the MCP spec: close the server’s input, wait for exit, send SIGTERM if needed, and SIGKILL as a last resort.

Motivation and Context

The MCP spec recommends a careful shutdown sequence for stdio transport to avoid leaving orphaned or stuck server processes. This change follows those steps, making shutdowns more reliable and predictable.

How Has This Been Tested?

All tests pass (except one unrelated skipped test).

Breaking Changes

None

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

Additional context

None

davenpi avatar May 21 '25 00:05 davenpi

Hey @ihrpr! Do you have a sec to review this PR? It makes the SDK follow the stdio shutdown procedure to the letter!

davenpi avatar May 22 '25 12:05 davenpi

related PR: https://github.com/modelcontextprotocol/python-sdk/pull/555

ihrpr avatar May 28 '25 08:05 ihrpr

Thank you again @davenpi for submitting this PR.

I spent yesterday and today trying to unify all the different approaches and fixes we have pending in this process termination space at the moment, as there are several interrelated fixes that either conflict or depend on each other - specifically #555, #729, #765, and #850.

I've added your change to #1044 as a draft with you as a co-author + added extensive regression testing. Would you be OK with consolidating this change into #1044 for the comprehensive testing & process handling introduced there?

felixweinberger avatar Jul 01 '25 18:07 felixweinberger

Thanks @felixweinberger for taking the time to coordinate all this! Consolidation sounds good.

davenpi avatar Jul 01 '25 20:07 davenpi

Thank you @davenpi! Will close this one and we can continue in #1091

ihrpr avatar Jul 09 '25 06:07 ihrpr