-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix: prevent stdio_server from closing process stdio handles #2040
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
Open
adityuhkapoor
wants to merge
7
commits into
modelcontextprotocol:main
Choose a base branch
from
adityuhkapoor:fix-stdio-preserve-process-handles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−10
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2be9d0
fix: prevent stdio_server from closing process stdio handles
adityuhkapoor d990d43
Merge branch 'main' into fix-stdio-preserve-process-handles
adityuhkapoor 685115b
use closefd=False per review feedback
adityuhkapoor ee156dd
remove filterwarnings no longer needed
adityuhkapoor 2819c02
Merge branch 'main' into fix-stdio-preserve-process-handles
adityuhkapoor 99239b6
close wrappers on exit to fix ResourceWarning
adityuhkapoor c25072f
Merge branch 'main' into fix-stdio-preserve-process-handles
adityuhkapoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """Test for issue #1933: stdio_server closes real process stdio handles.""" | ||
|
|
||
| import gc | ||
| import io | ||
| import os | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| from mcp.server.stdio import stdio_server | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") | ||
| async def test_stdio_server_preserves_process_handles(): | ||
| """After stdio_server() exits, the underlying stdin/stdout fds should still be open. | ||
|
|
||
| Before the fix, TextIOWrapper took ownership of sys.stdin.buffer and | ||
| sys.stdout.buffer. When the wrapper was garbage-collected, it closed the | ||
| underlying buffer, permanently killing process stdio. | ||
| """ | ||
| # Create real pipes to stand in for process stdin/stdout. | ||
| # Real fds are required because the bug involves TextIOWrapper closing | ||
| # the underlying fd — StringIO doesn't have file descriptors. | ||
| stdin_r_fd, stdin_w_fd = os.pipe() | ||
| stdout_r_fd, stdout_w_fd = os.pipe() | ||
|
|
||
| fake_stdin = io.TextIOWrapper(io.BufferedReader(io.FileIO(stdin_r_fd, "rb"))) | ||
| fake_stdout = io.TextIOWrapper(io.BufferedWriter(io.FileIO(stdout_w_fd, "wb"))) | ||
|
|
||
| saved_stdin, saved_stdout = sys.stdin, sys.stdout | ||
| sys.stdin = fake_stdin | ||
| sys.stdout = fake_stdout | ||
|
|
||
| # Close write end so stdin_reader gets EOF immediately | ||
| os.close(stdin_w_fd) | ||
|
|
||
| try: | ||
| async with stdio_server() as (read_stream, write_stream): | ||
| await write_stream.aclose() | ||
|
|
||
| await read_stream.aclose() | ||
| gc.collect() | ||
|
|
||
| # os.fstat raises OSError if the fd was closed | ||
| os.fstat(stdin_r_fd) | ||
| os.fstat(stdout_w_fd) | ||
| finally: | ||
| sys.stdin = saved_stdin | ||
| sys.stdout = saved_stdout | ||
| for fd in [stdin_r_fd, stdout_r_fd, stdout_w_fd]: | ||
| try: | ||
| os.close(fd) | ||
| except OSError: # pragma: no cover | ||
| pass |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
closefd=Falsegets you the same isolation without having to allocate anfd, so there's nothing to clean up and the filterwarnings on the test can be deleted.also maybe update the comment above to something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied, dropping import os and filter warnings, much appreciated!