Skip to content

Commit bbbc6cc

Browse files
Fix test_stdio_client_universal_cleanup for Windows
Replace platform-specific commands (ping/sleep) with a Python script to ensure consistent behavior across all platforms. Also adjust timing expectations to account for the full shutdown sequence on Windows: - 2 seconds waiting for stdin closure response - 2 seconds waiting for terminate() to complete - Plus overhead This prevents the test from timing out on Windows while still properly testing the cleanup mechanism.
1 parent 954020b commit bbbc6cc

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

tests/client/test_stdio.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,42 +105,50 @@ async def test_stdio_client_universal_cleanup():
105105
even when connected to processes that exit slowly.
106106
"""
107107

108-
# Use a simple sleep command that's available on all platforms
109-
# This simulates a process that takes time to terminate
110-
if sys.platform == "win32":
111-
# Windows: use ping with timeout to simulate a running process
112-
server_params = StdioServerParameters(
113-
command="ping",
114-
args=["127.0.0.1", "-n", "10"], # Ping 10 times, takes ~10 seconds
115-
)
116-
else:
117-
# Unix: use sleep command
118-
server_params = StdioServerParameters(
119-
command="sleep",
120-
args=["10"], # Sleep for 10 seconds
121-
)
108+
# Use a Python script that simulates a long-running process
109+
# This ensures consistent behavior across platforms
110+
long_running_script = textwrap.dedent(
111+
"""
112+
import time
113+
import sys
114+
115+
# Simulate a long-running process
116+
for i in range(100):
117+
time.sleep(0.1)
118+
# Flush to ensure output is visible
119+
sys.stdout.flush()
120+
sys.stderr.flush()
121+
"""
122+
)
123+
124+
server_params = StdioServerParameters(
125+
command=sys.executable,
126+
args=["-c", long_running_script],
127+
)
122128

123129
start_time = time.time()
124130

125131
# Use move_on_after which is more reliable for cleanup scenarios
126-
with anyio.move_on_after(6.0) as cancel_scope:
132+
# Increased timeout to account for Windows process termination overhead
133+
with anyio.move_on_after(8.0) as cancel_scope:
127134
async with stdio_client(server_params) as (read_stream, write_stream):
128135
# Immediately exit - this triggers cleanup while process is still running
129136
pass
130137

131138
end_time = time.time()
132139
elapsed = end_time - start_time
133140

134-
# Key assertion: Should complete quickly due to timeout mechanism
135-
assert elapsed < 5.0, (
136-
f"stdio_client cleanup took {elapsed:.1f} seconds, expected < 5.0 seconds. "
141+
# Key assertion: Should complete within reasonable time due to timeout mechanism
142+
# On Windows: 2s (stdin wait) + 2s (terminate wait) + overhead = ~5s expected
143+
assert elapsed < 6.0, (
144+
f"stdio_client cleanup took {elapsed:.1f} seconds, expected < 6.0 seconds. "
137145
f"This suggests the timeout mechanism may not be working properly."
138146
)
139147

140148
# Check if we timed out
141149
if cancel_scope.cancelled_caught:
142150
pytest.fail(
143-
"stdio_client cleanup timed out after 6.0 seconds. "
151+
"stdio_client cleanup timed out after 8.0 seconds. "
144152
"This indicates the cleanup mechanism is hanging and needs fixing."
145153
)
146154

0 commit comments

Comments
 (0)