diff --git a/tests/client/test_sse.py b/tests/client/test_sse.py index b0c9199b20..91530cf0d8 100644 --- a/tests/client/test_sse.py +++ b/tests/client/test_sse.py @@ -117,17 +117,16 @@ async def nested_sse_server(): ws="websockets-sansio", ) - server_task = asyncio.create_task(uvicorn.Server(config).serve()) + uvicorn_server = uvicorn.Server(config) + server_task = asyncio.create_task(uvicorn_server.serve()) await asyncio.sleep(0.1) try: yield f"http://127.0.0.1:{port}/nest-outer/nest-inner/mcp/sse/" finally: - server_task.cancel() - try: - await server_task - except asyncio.CancelledError: - pass + # Graceful shutdown - required for uvicorn 0.39+ due to context isolation + uvicorn_server.should_exit = True + await server_task async def test_run_server_on_path(sse_server_custom_path: str): diff --git a/tests/client/test_streamable_http.py b/tests/client/test_streamable_http.py index 5c1753d9af..1d301dc378 100644 --- a/tests/client/test_streamable_http.py +++ b/tests/client/test_streamable_http.py @@ -127,17 +127,15 @@ async def nested_server(): ) # Use the simple asyncio pattern - server_task = asyncio.create_task(uvicorn.Server(config).serve()) + uvicorn_server = uvicorn.Server(config) + server_task = asyncio.create_task(uvicorn_server.serve()) await asyncio.sleep(0.1) yield f"http://127.0.0.1:{port}/nest-outer/nest-inner/final/mcp" - # Cleanup - server_task.cancel() - try: - await server_task - except asyncio.CancelledError: - pass + # Graceful shutdown - required for uvicorn 0.39+ due to context isolation + uvicorn_server.should_exit = True + await server_task async def test_ping(streamable_http_server: str): diff --git a/tests/server/test_logging.py b/tests/server/test_logging.py index cfc376c0b0..74d9fb76f6 100644 --- a/tests/server/test_logging.py +++ b/tests/server/test_logging.py @@ -49,9 +49,13 @@ async def test_uvicorn_logging_default_level( ) mock_server_instance.serve.assert_awaited_once() + # Signal the mock to finish and cancel with timeout + serve_finished_event.set() server_task.cancel() - with pytest.raises(asyncio.CancelledError): - await server_task + try: + await asyncio.wait_for(server_task, timeout=2.0) + except (asyncio.CancelledError, asyncio.TimeoutError): + pass @patch("fastmcp.server.server.uvicorn.Server") @@ -109,9 +113,13 @@ async def test_uvicorn_logging_with_custom_log_config( ) mock_server_instance.serve.assert_awaited_once() + # Signal the mock to finish and cancel with timeout + serve_finished_event.set() server_task.cancel() - with pytest.raises(asyncio.CancelledError): - await server_task + try: + await asyncio.wait_for(server_task, timeout=2.0) + except (asyncio.CancelledError, asyncio.TimeoutError): + pass @patch("fastmcp.server.server.uvicorn.Server") @@ -172,6 +180,10 @@ async def test_uvicorn_logging_custom_log_config_overrides_log_level_param( ) mock_server_instance.serve.assert_awaited_once() + # Signal the mock to finish and cancel with timeout + serve_finished_event.set() server_task.cancel() - with pytest.raises(asyncio.CancelledError): - await server_task + try: + await asyncio.wait_for(server_task, timeout=2.0) + except (asyncio.CancelledError, asyncio.TimeoutError): + pass