| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
… exits When using transport="stdio", the server was closing sys.stdin.buffer and sys.stdout.buffer when exiting, causing subsequent stdio operations to fail with ValueError: I/O operation on closed file. This fix uses os.dup() to create duplicate file descriptors, so the original stdin/stdout remain open when the wrapper streams are closed. Fixes modelcontextprotocol#1933
|
Closing as a duplicate of #2040, which uses the closefd=False approach discussed in review there. Thanks for the contribution. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Fixes #1933
Problem
When using transport="stdio", the server closes sys.stdin.buffer and sys.stdout.buffer when exiting, causing subsequent stdio operations to fail with:
Root Cause
The stdio_server() function wraps sys.stdin.buffer and sys.stdout.buffer directly. When these wrappers are closed (when the context manager exits), they also close the original system streams.
Solution
Use os.dup() to create duplicate file descriptors before wrapping:
This ensures the original stdin/stdout remain open when the wrapper streams are closed.
Testing
Tested with the reproduction case from the issue:
Pressing Ctrl+D to exit the server no longer causes "I/O operation on closed file" error.
This fix implements the proposed solution from the original issue.