From 388ec1e6fcb463d139e22a01e63efe909d64e241 Mon Sep 17 00:00:00 2001 From: Mac Howe Date: Sat, 11 Jul 2026 13:17:16 -0400 Subject: [PATCH] fix: Prevent synchronous code executors from blocking the event loop Offload the synchronous BaseCodeExecutor.execute_code calls in the code execution pre- and post-processors to a worker thread with asyncio.to_thread, so a blocking executor no longer freezes the asyncio event loop. Fixes https://github.com/google/adk-python/issues/6353 --- .../adk/flows/llm_flows/_code_execution.py | 7 +- .../flows/llm_flows/test_code_execution.py | 127 ++++++++++++++++++ 2 files changed, 132 insertions(+), 2 deletions(-) diff --git a/src/google/adk/flows/llm_flows/_code_execution.py b/src/google/adk/flows/llm_flows/_code_execution.py index 36ad84fce04..2e70ae15ecb 100644 --- a/src/google/adk/flows/llm_flows/_code_execution.py +++ b/src/google/adk/flows/llm_flows/_code_execution.py @@ -16,6 +16,7 @@ from __future__ import annotations +import asyncio import base64 import copy import dataclasses @@ -245,7 +246,8 @@ async def _run_pre_processor( content=code_content, ) - code_execution_result = code_executor.execute_code( + code_execution_result = await asyncio.to_thread( + code_executor.execute_code, invocation_context, CodeExecutionInput( code=code_str, @@ -355,7 +357,8 @@ async def _run_post_processor( actions=EventActions(), ) - code_execution_result = code_executor.execute_code( + code_execution_result = await asyncio.to_thread( + code_executor.execute_code, invocation_context, CodeExecutionInput( code=code_str, diff --git a/tests/unittests/flows/llm_flows/test_code_execution.py b/tests/unittests/flows/llm_flows/test_code_execution.py index 3acd6f13d25..d6d499f7d48 100644 --- a/tests/unittests/flows/llm_flows/test_code_execution.py +++ b/tests/unittests/flows/llm_flows/test_code_execution.py @@ -15,7 +15,10 @@ """Unit tests for Code Execution logic.""" import ast +import asyncio import datetime +import threading +from typing import Optional from unittest.mock import AsyncMock from unittest.mock import MagicMock from unittest.mock import patch @@ -23,11 +26,14 @@ from google.adk.agents.llm_agent import Agent from google.adk.code_executors.base_code_executor import BaseCodeExecutor from google.adk.code_executors.built_in_code_executor import BuiltInCodeExecutor +from google.adk.code_executors.code_execution_utils import CodeExecutionInput from google.adk.code_executors.code_execution_utils import CodeExecutionResult from google.adk.code_executors.code_execution_utils import File from google.adk.flows.llm_flows._code_execution import _DATA_FILE_HELPER_LIB from google.adk.flows.llm_flows._code_execution import _get_data_file_preprocessing_code +from google.adk.flows.llm_flows._code_execution import request_processor from google.adk.flows.llm_flows._code_execution import response_processor +from google.adk.models.llm_request import LlmRequest from google.adk.models.llm_response import LlmResponse from google.genai import types import pytest @@ -35,6 +41,39 @@ from ... import testing_utils +class _ExecutionRecord: + """Captures how the executor ran, for cross-thread inspection in tests.""" + + def __init__(self): + self.thread: Optional[threading.Thread] = None + self.released: Optional[bool] = None + + +class _RecordingCodeExecutor(BaseCodeExecutor): + """A code executor that records the thread it runs on. + + `execute_code` blocks on `release` so tests can verify it is offloaded from + the event loop: it records the running thread, signals `started`, then waits + for `release` before returning. + """ + + model_config = {'arbitrary_types_allowed': True} + + started: threading.Event + release: threading.Event + record: _ExecutionRecord + + def execute_code( + self, + invocation_context, + code_execution_input: CodeExecutionInput, + ) -> CodeExecutionResult: + self.record.thread = threading.current_thread() + self.started.set() + self.record.released = self.release.wait(timeout=2) + return CodeExecutionResult(stdout='ok') + + @pytest.mark.asyncio @patch('google.adk.flows.llm_flows._code_execution.datetime') async def test_builtin_code_executor_image_artifact_creation(mock_datetime): @@ -207,3 +246,91 @@ def test_get_data_file_preprocessing_code_injection_reproduction(): break assert read_csv_arg == bad_filename + + +@pytest.mark.asyncio +async def test_post_processor_does_not_block_event_loop(): + """Response processor offloads blocking execute_code off the event loop.""" + started = threading.Event() + release = threading.Event() + record = _ExecutionRecord() + loop_ran = False + code_executor = _RecordingCodeExecutor( + started=started, release=release, record=record + ) + agent = Agent(name='test_agent', code_executor=code_executor) + invocation_context = await testing_utils.create_invocation_context( + agent=agent, user_content='test message' + ) + invocation_context.artifact_service = MagicMock() + invocation_context.artifact_service.save_artifact = AsyncMock() + + llm_response = LlmResponse( + content=types.Content( + parts=[types.Part(text='```python\nprint("hello")\n```')] + ) + ) + + async def _release_when_started(): + nonlocal loop_ran + while not started.is_set(): + await asyncio.sleep(0.001) + loop_ran = True + release.set() + + releaser = asyncio.create_task(_release_when_started()) + _ = [ + event + async for event in response_processor.run_async( + invocation_context, llm_response + ) + ] + await releaser + + assert record.thread is not threading.main_thread() + assert record.released is True + assert loop_ran is True + + +@pytest.mark.asyncio +async def test_pre_processor_runs_execute_code_off_the_loop(): + """Request processor offloads blocking execute_code off the event loop.""" + started = threading.Event() + release = threading.Event() + release.set() + record = _ExecutionRecord() + code_executor = _RecordingCodeExecutor( + started=started, + release=release, + record=record, + optimize_data_file=True, + ) + agent = Agent(name='test_agent', code_executor=code_executor) + invocation_context = await testing_utils.create_invocation_context( + agent=agent, user_content='test message' + ) + + llm_request = LlmRequest( + contents=[ + types.Content( + role='user', + parts=[ + types.Part( + inline_data=types.Blob( + mime_type='text/csv', + data=b'col1,col2\n1,2\n', + ) + ) + ], + ) + ] + ) + + _ = [ + event + async for event in request_processor.run_async( + invocation_context, llm_request + ) + ] + + assert record.thread is not threading.main_thread()