Fix: Resolve CancelScope error in McpToolset session creation (issue #5729)#5827
Draft
9chait9 wants to merge 2 commits into
Draft
Fix: Resolve CancelScope error in McpToolset session creation (issue #5729)#58279chait9 wants to merge 2 commits into
9chait9 wants to merge 2 commits into
Conversation
…onContext._run to prevent CancelScope errors. This resolves issue google#5729.
Collaborator
|
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.