Skip to content

fix(server): prevent stdio transport from closing original stdio handles#2424

Closed
goingforstudying-ctrl wants to merge 1 commit intomodelcontextprotocol:mainfrom
goingforstudying-ctrl:fix-stdio-close
Closed

fix(server): prevent stdio transport from closing original stdio handles#2424
goingforstudying-ctrl wants to merge 1 commit intomodelcontextprotocol:mainfrom
goingforstudying-ctrl:fix-stdio-close

Conversation

@goingforstudying-ctrl
Copy link
Copy Markdown

Description

When using , the context manager wraps and in . When these wrappers are closed (either explicitly or when exiting the context), they also close the underlying buffers, causing subsequent stdio operations to fail with .

Changes

This PR duplicates the file descriptors before wrapping them using , so that the original stdin/stdout remain open after the transport is closed.

Before (problem):

After (fixed):

Technical Details

  • Uses to create duplicate file descriptors
  • Opens duplicates with with
  • Wraps the duplicates in and
  • Original stdin/stdout remain unaffected when transport closes

Fixes #1933

/cc @maintainers

@goingforstudying-ctrl
Copy link
Copy Markdown
Author

Hi @jspahrsummers, just checking in on this PR. Please let me know if any changes are needed. Happy to address any feedback!

@goingforstudying-ctrl
Copy link
Copy Markdown
Author

Hi @maintainer, just checking in on this PR. Please let me know if any changes are needed. Happy to address any feedback!

1 similar comment
@goingforstudying-ctrl
Copy link
Copy Markdown
Author

Hi @maintainer, just checking in on this PR. Please let me know if any changes are needed. Happy to address any feedback!

@goingforstudying-ctrl
Copy link
Copy Markdown
Author

Hi @jspahrsummers and team,

Just checking in on this PR again. It's been a few days since the last ping. The fix prevents stdio transport from closing original stdio handles, which could cause issues for consumers that need to continue using stdin/stdout after the MCP client/server shuts down.

Please let me know if:

  • Any changes are needed to the implementation
  • More context or test cases would help
  • There's anything I can do to move this forward

Happy to address any feedback. Thanks!

@goingforstudying-ctrl
Copy link
Copy Markdown
Author

Fixed! Added the missing to — the exception was referenced but wasn't imported. This should resolve the pre-commit / test failures. Please let me know if anything else is needed.

@goingforstudying-ctrl
Copy link
Copy Markdown
Author

Fixed! Added the missing import io to src/mcp/server/stdio.py — the io.UnsupportedOperation exception was referenced but io wasn't imported. This should resolve the pre-commit / test failures. Please let me know if anything else is needed.

@goingforstudying-ctrl
Copy link
Copy Markdown
Author

Fixed the import order issue — wanted before (isort rule). The pre-commit failure was just a formatting fix, not a logic error. All tests pass. Ready for another look when you have a moment!

Signed-off-by: goingforstudying-ctrl <goingforstudying-ctrl@users.noreply.github.com>
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.

Using transport="stdio" closes real stdio, causing ValueError after server exits

1 participant