Fix: Resolve CancelScope error in McpToolset session creation (issue #5729)#5827
Fix: Resolve CancelScope error in McpToolset session creation (issue #5729)#58279chait9 wants to merge 9 commits into
Conversation
…onContext._run to prevent CancelScope errors. This resolves issue google#5729.
|
Response from ADK Triaging Agent Hello @9chait9, thank you for creating this PR! We appreciate your contribution to resolving the Currently, the following sections are missing or incomplete in your PR description:
Thank you for your help in keeping the codebase robust! |
drs-merge-operation-id: c80e0bdaa4dc0a1eb918d514a009cbd7f5ca6f4cd46ddf52fdddcc26fb4180f6
|
/adk-pr-analyze |
|
I have completed a thorough, read-only analysis of Pull Request #5827. Here is the premium PR Analysis Report highlighting the critical findings, alignment details, and recommendations. 🔍 ADK Pull Request Analysis: PR #5827Title: Fix: Resolve CancelScope error in McpToolset session creation (issue #5729) Executive Summary
Detailed Findings & Analysis1. Objectives & Impact ("What does it do?")
2. Justification & Value ("Is it a valid and useful change?")
3. Principle & Style Alignment Checklist ("Does it follow rules?")
📝 Summary of Work & Recommendations
|
Fixes #5729
This PR addresses an intermittent
RuntimeError: Attempted to exit cancel scope in a different taskthat occurs during MCP session creation whenLlmAgents are served viato_a2a()with multipleMcpToolsets usingStreamableHTTPServerParams. The root cause isasyncio.wait_forcreating a nested task around theAnyIOCancelScopeentry, leading to the cancellation happening from a different task.The fix involves removing the problematic
asyncio.wait_forwrapper aroundexit_stack.enter_async_context(self._client)inSessionContext._run. This ensures that theCancelScopeis always entered and exited within the same task, satisfyingAnyIO's requirements. The connection-establishment timeout is still enforced byMCPSessionManager.create_sessionvia its outerasyncio.wait_for.Test Plan
Objective: Verify that the
RuntimeError: Attempted to exit cancel scope in a different taskno longer occurs when using multipleMcpToolsets withStreamableHTTPServerParamsunder concurrent load.Scope:
src/google/adk/tools/mcp_tool/session_context.pymodifications.MCPSessionManager.create_session.LlmAgentsession creation withto_a2a()when configured with multipleMcpToolsets andStreamableHTTPServerParams.Test Cases:
Unit Test for
SessionContext._run(New or Modified):_clientforSessionContextthat simulates a delayedenter_async_context.asyncio.wait_foraroundexit_stack.enter_async_context(self._client)should not be present.RuntimeError: Attempted to exit cancel scope in a different taskshould not be raised when the context entry eventually completes or is cancelled externally.MCPSessionManager.create_sessionshould still enforce the overall connection establishment timeout.asyncio.wait_forand its impact onCancelScopebehavior.Integration Test: Multiple
LlmAgents withMcpToolsetandStreamableHTTPServerParams:LlmAgentinstances, each served viato_a2a().LlmAgentshould use at least oneMcpToolset.StreamableHTTPServerParamsshould be configured for theMcpToolsets.LlmAgentsessions should be created successfully withoutRuntimeErrororValueError: Tool '' not found.Negative Test:
StreamableHTTPServerParamswith_MCP_GRACEFUL_ERROR_HANDLING(if applicable):_MCP_GRACEFUL_ERROR_HANDLINGfeature flag for testing, run the integration test (Test Case 2) with the flag both enabled and disabled (if the old code path can still be triggered).asyncio.wait_foris being removed entirely.Testing Environment:
LlmAgentandMcpToolsetinteractions.google/adk-pythonrepository with the proposed changes.Monitoring and Logging:
RuntimeError: Attempted to exit cancel scope in a different taskorValueError: Tool '' not foundmessages.logger.debug('Session has been successfully initialized')appears as expected for all sessions.Regression Testing:
McpToolsetandLlmAgentfunctionality continue to pass without introducing new regressions.