Fix test cleanup for uvicorn 0.39+ context isolation#2696
Conversation
Test Failure AnalysisSummary: One test is failing: Root Cause: In Suggested Solution: Add an exception handler for FastMCP's error types before the generic # In src/fastmcp/tools/tool_manager.py, around line 161
try:
return await tool.run(arguments)
except ValidationError as e: # Pydantic ValidationError
logger.exception(f"Error validating tool {key!r}: {e}")
raise e
except ToolError as e: # Already a ToolError
logger.exception(f"Error calling tool {key!r}")
raise e
except FastMCPError as e: # Add this handler for ValidationError, ResourceError, etc.
logger.exception(f"Error calling tool {key!r}")
raise e
except Exception as e: # Generic exceptions
logger.exception(f"Error calling tool {key!r}")
if self.mask_error_details:
raise ToolError(f"Error calling tool {key!r}") from e
else:
raise ToolError(f"Error calling tool {key!r}: {e}") from eThis ensures that FastMCP's Detailed AnalysisTest OutputCode Flow
Related Files
Related Files
Note: This failure is unrelated to the uvicorn context isolation fixes in this PR. The modified test files ( |
d79d30a to
cf2916b
Compare
|
I'm reverting that PR from Uvicorn, not sure how it impacts you. But please ping me if you have any questions. |
Uvicorn 0.39.0 introduced a fix for ContextVars pollution (encode/uvicorn#2742) that wraps ASGI tasks in empty contexts. This is a workaround for a CPython bug (python/cpython#140947) being fixed in Python 3.13+.
The context isolation breaks task cancellation propagation, causing test cleanup to hang when we
task.cancel()andawait taskon uvicorn server tasks.Fix: Use graceful shutdown (
server.should_exit = True) for real uvicorn servers, and add timeouts for mocked server cleanup.Files changed:
tests/server/test_logging.py- Add timeout to mocked server cleanuptests/client/test_streamable_http.py- Use graceful shutdown for nested_server fixturetests/client/test_sse.py- Use graceful shutdown for nested_sse_server fixture