diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.py index c3b6f7fbf..83223084b 100644 --- a/src/fastmcp/server/server.py +++ b/src/fastmcp/server/server.py @@ -465,10 +465,12 @@ class FastMCP(Generic[LifespanResultT]): try: yield finally: - # Cancel worker task on exit + # Cancel worker task on exit with timeout to prevent hanging worker_task.cancel() - with suppress(asyncio.CancelledError): - await worker_task + with suppress( + asyncio.CancelledError, asyncio.TimeoutError + ): + await asyncio.wait_for(worker_task, timeout=2.0) finally: _current_worker.reset(worker_token) finally: