Skip to content

Fix hanging on streams when stdio_client exiting #559

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
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mcp/client/stdio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ async def stdin_writer():
await terminate_windows_process(process)
else:
process.terminate()
read_stream.close()
write_stream.close()


def _get_executable_command(command: str) -> str:
Expand Down
13 changes: 13 additions & 0 deletions tests/client/test_stdio_cm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from mcp import StdioServerParameters
from mcp.client.stdio import stdio_client


@pytest.mark.anyio
async def test_context_manager_exiting():
async with stdio_client(StdioServerParameters(command="tee")) as (
read_stream,
write_stream,
):
pass
Loading