Add timeout to worker cleanup to prevent hanging on Windows

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 <noreply@anthropic.com>
This commit is contained in:
Chris Guidry 2025-12-05 11:52:43 -05:00
commit 53efaeba5b

View file

@ -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: