Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

fix: use simple sleep instead of _wait_for_port on Windows

Chris Guidry7e4c854625f8
diff --git a/src/fastmcp/utilities/tests.py b/src/fastmcp/utilities/tests.py
index 6b7b28194..d78ce3e62 100644
--- a/src/fastmcp/utilities/tests.py
+++ b/src/fastmcp/utilities/tests.py
@@ -22,40 +22,6 @@ if TYPE_CHECKING:
from fastmcp.server.server import FastMCP
-async def _wait_for_port(
- host: str, port: int, timeout: float = 5.0, interval: float = 0.01
-) -> None:
- """Wait for a port to accept connections.
-
- Args:
- host: Host to connect to
- port: Port to probe
- timeout: Maximum time to wait in seconds
- interval: Time between connection attempts
- """
- import asyncio
- import sys
-
- start = asyncio.get_running_loop().time()
- while True:
- try:
- _, writer = await asyncio.wait_for(
- asyncio.open_connection(host, port), timeout=interval
- )
- writer.close()
- # On Windows, wait_closed() can hang due to ProactorEventLoop socket
- # shutdown issues. Skip it - the socket will be cleaned up eventually.
- if sys.platform != "win32":
- await writer.wait_closed()
- return
- except (OSError, asyncio.TimeoutError):
- if asyncio.get_running_loop().time() - start > timeout:
- raise TimeoutError(
- f"Port {port} on {host} not available after {timeout}s"
- ) from None
- await asyncio.sleep(interval)
-
-
@contextmanager
def temporary_settings(**kwargs: Any):
"""
@@ -246,9 +212,8 @@ async def run_server_async(
# Wait for server lifespan to be ready
await server._started.wait()
- # Wait for HTTP port to be listening (lifespan is ready but uvicorn
- # may not have bound yet)
- await _wait_for_port(host, port)
+ # Give uvicorn a moment to bind the port after lifespan is ready
+ await asyncio.sleep(0.1)
try:
yield f"http://{host}:{port}{path}"