From 53efaeba5bdce53138ac349bc53f3c00c0f027ce Mon Sep 17 00:00:00 2001 From: Chris Guidry Date: Fri, 5 Dec 2025 11:52:43 -0500 Subject: [PATCH] Add timeout to worker cleanup to prevent hanging on Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When pytest-xdist terminates worker processes on Windows, the event loop may close before cleanup can complete. Adding a 2-second timeout to the worker cancellation prevents indefinite hanging. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/fastmcp/server/server.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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: