#5325·fastmcp

run_server_async hangs instead of raising when the server fails to start

Author: asasemahmedCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbugtests

What happened?

If the server passed to run_server_async fails during startup (for example, its lifespan raises), the context manager never yields and never raises. It waits until an outer timeout fires, and the original exception only gets printed later as "Task exception was never retrieved".

Example Code

python
import asyncio
from contextlib import asynccontextmanager

from fastmcp import FastMCP
from fastmcp.utilities.tests import run_server_async


@asynccontextmanager
async def lifespan(server):
    raise RuntimeError("database unavailable")
    yield


mcp = FastMCP("demo", lifespan=lifespan)


async def main():
    try:
        async with asyncio.timeout(5):
            async with run_server_async(mcp) as url:
                print("started", url)
    except TimeoutError:
        print("TimeoutError: run_server_async still waiting after 5s")
    except Exception as e:
        print(f"{type(e).__name__}: {e}")


asyncio.run(main())

Output:

TimeoutError: run_server_async still waiting after 5s
Task exception was never retrieved
future: <Task finished name='Task-2' coro=<TransportMixin.run_http_async() done, ...> exception=RuntimeError('database unavailable')>

Version Information

FastMCP version:                                          4.0.11.dev11+89b00b7f
MCP version:                                                              2.0.0
Python version:                                                         3.12.14
Platform:                                             Windows-11-10.0.26200-SP0