Skip to content

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

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

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

When using transport='stdio', the stdio_server context manager wraps
sys.stdin.buffer and sys.stdout.buffer in TextIOWrapper. 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 ValueError.

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

Fixes modelcontextprotocol#1933
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