Skip to content

#552 #707

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 14 commits into
base: main
Choose a base branch
from
Open

#552 #707

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
50 changes: 50 additions & 0 deletions tests/issues/test_552_windows_hang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import asyncio
import sys

import pytest

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

# @pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be uncommented? Tests do seem to fail on Ubuntu

@pytest.mark.skipif(sys.version_info < (3, 11), reason="asyncio.timeout in 3.11+")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you switch to anyio like recommended below, you shouldn't need this skip case anymore.

@pytest.mark.parametrize(
"args",
[
["/C", "echo", '{"jsonrpc": "2.0", "id": 1, "result": null}'],
["dfghfgh"],
["/C", "echo"],
],
)
@pytest.mark.anyio
async def test_windows_process_creation(args):
"""
Test that directly tests the process creation function that was fixed in issue #552.
This simpler test verifies that Windows process creation works without hanging.
"""
# Use a simple command that should complete quickly on Windows
params = StdioServerParameters(
command="cmd",
# Echo a valid JSON-RPC response message that will be parsed correctly
args=args,
)

# Directly test the fixed function that was causing the hanging issue
try:
# Set a timeout to prevent hanging
async with asyncio.timeout(5):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use anyio.timeout or anyio.fail_after here please to align with other tests?

# Test the actual process creation function that was fixed
async with stdio_client(params) as (read, write):
print("inside client")
async with ClientSession(read, write) as c:
print("inside ClientSession")
await c.initialize()

except asyncio.TimeoutError:
pytest.xfail("Process creation timed out, indicating a hang issue")
except ProcessLookupError:
pytest.xfail("Process creation failed with ProcessLookupError")
except Exception as e:
assert "ExceptionGroup" in repr(e), f"Unexpected error: {e}"
assert "ProcessLookupError" in repr(e), f"Unexpected error: {e}"
pytest.xfail(f"Expected error: {e}")
Comment on lines +32 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you simplify to use anyio.fail_after(5) like so?

      # Will fail with TimeoutError if it hangs
      with anyio.fail_after(5):
          async with stdio_client(params) as (read, write):
              print("inside client")
              async with ClientSession(read, write) as c:
                  print("inside ClientSession")
                  await c.initialize()

Then you don't need to do all the except statements either.

Loading