Skip to content

run_server_async hangs instead of raising when the server fails to start #5325

Closed

Description

@asasemahmed

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

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

Activity

  1. added
    bug
    on Sep 28, 2026
  2. added a commit that references this issue on Oct 5, 2026
    e4e5999
  3. added a commit that references this issue on Oct 5, 2026
    f0ed4fb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtests

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions