Files
modelcontextprotocol--pytho…/tests/issues/test_552_windows_hang.py
T
wehub-resource-sync 49b9bb6724
Deploy Docs / deploy-docs (push) Failing after 1s
Conformance Tests / client-conformance (push) Failing after 3s
Conformance Tests / server-conformance (push) Failing after 1s
GitHub Actions Security Analysis / zizmor (push) Failing after 1s
CI / checks (push) Failing after 59m20s
CI / all-green (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:10:27 +08:00

58 lines
1.9 KiB
Python

"""Test for issue #552: stdio_client hangs on Windows."""
import json
import sys
from textwrap import dedent
import anyio
import pytest
from mcp_types import InitializeResult
from mcp_types.version import LATEST_HANDSHAKE_VERSION
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
@pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test") # pragma: no cover
@pytest.mark.anyio
async def test_initialize_succeeds_and_shutdown_returns_after_the_server_exits_mid_session():
"""Initialize completes and shutdown returns when the server exits mid-session.
This is the proactor pipe scenario that hung on Windows 11 (issue #552). The positive
assertion matters: a session that errors quickly would also "not hang".
"""
# A minimal server: answer initialize correctly, then exit.
server_script = dedent(f"""
import json
import sys
line = sys.stdin.readline()
request = json.loads(line)
response = {{
"jsonrpc": "2.0",
"id": request["id"],
"result": {{
"protocolVersion": {json.dumps(LATEST_HANDSHAKE_VERSION)},
"capabilities": {{}},
"serverInfo": {{"name": "test-server", "version": "1.0"}}
}}
}}
print(json.dumps(response))
sys.stdout.flush()
""").strip()
params = StdioServerParameters(
command=sys.executable,
args=["-c", server_script],
)
with anyio.fail_after(10):
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
result = await session.initialize()
assert isinstance(result, InitializeResult)
assert result.server_info.name == "test-server"
# Exiting ClientSession and stdio_client must not hang even though the
# server process is already gone.