Repository navigation
Conversation
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 google#6353
Contributor
Author
|
/gemini review |
Collaborator
FrigaZzz
pushed a commit
to FrigaZzz/adk-python
that referenced
this pull request
Aug 11, 2026
Merge google#6375 Offload blocking execute_code calls in pre/post processors to a worker thread using asyncio.to_thread to prevent freezing the asyncio event loop. Fixes google#6353 PiperOrigin-RevId: 946359094
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
_run_pre_processorand_run_post_processorinflows/llm_flows/_code_execution.pycall the synchronousBaseCodeExecutor.execute_code(...)directly on the event loop. Any blocking executor (e.g.AgentEngineSandboxCodeExecutor, whose HTTP calls can take minutes) freezes the entire asyncio loop for the duration: FastAPI health checks stop responding, other sessions' SSE streams stall, and Kubernetes may kill the pod.Solution:
Offload both
execute_codecalls to a worker thread withawait asyncio.to_thread(...), the same pattern already used for blocking I/O elsewhere in the codebase (gcs_artifact_service.py,file_artifact_service.py). Argument construction and allCodeExecutorContextstate updates stay on the loop thread; only the executor's blocking body runs on the worker thread. No public API changes, sync executors keep working unchanged, and an async-nativeexecute_code_asynchook could still be layered on later for executors that want a real async client.Testing Plan
Unit Tests:
Two regression tests added to
tests/unittests/flows/llm_flows/test_code_execution.py:test_post_processor_does_not_block_event_loop- a stub executor blocks on athreading.Eventthat only a concurrently scheduled asyncio task can set. Ifexecute_coderan on the loop thread, the loop would be frozen and the release could never happen (bounded by a 2s timeout, so a regression fails fast instead of hanging CI). Also asserts execution happened off the main thread.test_pre_processor_runs_execute_code_off_the_loop- drives the data-file pre-processing path (optimize_data_file=Truewith an inline CSV) and assertsexecute_coderuns off the main thread.Both tests fail on the unfixed code and pass with the fix:
Manual End-to-End (E2E) Tests:
To reproduce the original issue: run
adk api_serverwith an agent using a slow/blocking code executor (or anyBaseCodeExecutorsubclass whoseexecute_codesleeps), trigger a code execution, and concurrently hit any other endpoint (e.g. the health/list-apps route). Before this change the concurrent request stalls until the execution finishes; with this change it responds immediately. The unit tests above encode this exact scenario deterministically (blocked executor + concurrent loop task).Checklist
Additional context
asyncio.to_threaduses the default thread pool, matching the existing convention for blocking I/O in this repo. If executors with native async clients become common, a follow-up could addBaseCodeExecutor.execute_code_async(defaulting toasyncio.to_thread(self.execute_code, ...)) so implementations can override it without a breaking change.