From 2f235a6137a58660ffd491f0e528d222e570087c Mon Sep 17 00:00:00 2001 From: Abdulrehman Satkut Date: Sat, 5 Dec 2026 10:22:21 +0530 Subject: [PATCH] fix(tools): cancel and await sibling toolset discovery tasks on cancellation (#8384) --- .../adk/flows/llm_flows/tools/_agent_tools.py | 27 ++++++----- .../llm_flows/test_tool_request_processors.py | 48 +++++++++++++++++++ 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/google/adk/flows/llm_flows/tools/_agent_tools.py b/src/google/adk/flows/llm_flows/tools/_agent_tools.py index 8ad06865427..2fa80f9dbde 100644 --- a/src/google/adk/flows/llm_flows/tools/_agent_tools.py +++ b/src/google/adk/flows/llm_flows/tools/_agent_tools.py @@ -31,6 +31,7 @@ from ....tools.base_toolset import BaseToolset from ....tools.tool_context import ToolContext from .._base_llm_processor import BaseLlmRequestProcessor +from ._batch_executor import _gather_or_cancel from ._live_caller import _is_streaming_tool if TYPE_CHECKING: @@ -82,21 +83,21 @@ async def process_agent_tools( from ....agents.llm_agent import _convert_tool_union_to_tools - # Resolve tool_unions in parallel. ``asyncio.gather`` preserves - # input order in the returned list, so the serial commit phase below - # still observes ``agent.tools`` order. If any resolution raises, - # gather cancels the siblings and propagates -- same observable - # behavior as the previous serial loop, which would propagate the - # first exception and abandon the rest. - resolved_tools_per_union = await asyncio.gather(*( - _convert_tool_union_to_tools( - tool_union, - ReadonlyContext(invocation_context), - model, - multiple_tools, + # Resolve tool_unions in parallel. ``_gather_or_cancel`` preserves + # input order in the returned list and cancels/awaits sibling tasks if any + # listing fails or raises ``asyncio.CancelledError``. + tasks = [ + asyncio.create_task( + _convert_tool_union_to_tools( + tool_union, + ReadonlyContext(invocation_context), + model, + multiple_tools, + ) ) for tool_union in agent.tools - )) + ] + resolved_tools_per_union = await _gather_or_cancel(tasks) # Serial commit phase, in original ``agent.tools`` order. Mutations # to ``llm_request`` and reads of its state (model, config.tools, diff --git a/tests/unittests/flows/llm_flows/test_tool_request_processors.py b/tests/unittests/flows/llm_flows/test_tool_request_processors.py index 499c2e200ff..52f8bcbe72b 100644 --- a/tests/unittests/flows/llm_flows/test_tool_request_processors.py +++ b/tests/unittests/flows/llm_flows/test_tool_request_processors.py @@ -110,3 +110,51 @@ async def test_processor_appended_to_request_processors_sees_no_tools_yet(): await _run_preprocess(flow, Agent(name='test_agent', tools=[_a_tool])) assert probe.seen_tool_names == [] + + +@pytest.mark.asyncio +async def test_process_agent_tools_cancels_siblings_when_listing_cancels(): + """Child-originated CancelledError settles and cancels sibling toolset listings.""" + import asyncio + from google.adk.tools.base_toolset import BaseToolset + + started = asyncio.Event() + release = asyncio.Event() + cleaned = asyncio.Event() + completed = asyncio.Event() + + class _WaitingToolset(BaseToolset): + + async def get_tools(self, readonly_context=None): + del readonly_context + started.set() + try: + await release.wait() + completed.set() + return [] + finally: + cleaned.set() + + async def close(self): + pass + + class _CancellingToolset(BaseToolset): + + async def get_tools(self, readonly_context=None): + del readonly_context + await started.wait() + raise asyncio.CancelledError('listing cancelled') + + async def close(self): + pass + + agent = Agent(name='test_agent', tools=[_WaitingToolset(), _CancellingToolset()]) + ctx = await testing_utils.create_invocation_context( + agent=agent, user_content='test message' + ) + + with pytest.raises(asyncio.CancelledError): + await _agent_tools.process_agent_tools(ctx, LlmRequest()) + + assert cleaned.is_set() + assert not completed.is_set()