Skip to content

fix: Prevent synchronous code executors from blocking the event loop - #6375

Closed
ItsMacto wants to merge 1 commit into
google:mainfrom
ItsMacto:fix-code-execution-blocking-event-loop
Closed

ItsMacto wants to merge 1 commit into
google:mainfrom
ItsMacto:fix-code-execution-blocking-event-loop

Conversation

@ItsMacto

@ItsMacto ItsMacto commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

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_processor and _run_post_processor in flows/llm_flows/_code_execution.py call the synchronous BaseCodeExecutor.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_code calls to a worker thread with await 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 all CodeExecutorContext state 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-native execute_code_async hook could still be layered on later for executors that want a real async client.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

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 a threading.Event that only a concurrently scheduled asyncio task can set. If execute_code ran 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=True with an inline CSV) and asserts execute_code runs off the main thread.

Both tests fail on the unfixed code and pass with the fix:

# before fix (source change reverted, tests kept):
FAILED test_post_processor_does_not_block_event_loop - assert <_MainThread(MainThread, ...)> is not <_MainThread(MainThread, ...)>
FAILED test_pre_processor_runs_execute_code_off_the_loop

# with fix:
$ pytest tests/unittests/flows/llm_flows/test_code_execution.py -q
6 passed in 3.50s

Manual End-to-End (E2E) Tests:

To reproduce the original issue: run adk api_server with an agent using a slow/blocking code executor (or any BaseCodeExecutor subclass whose execute_code sleeps), 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

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

asyncio.to_thread uses 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 add BaseCodeExecutor.execute_code_async (defaulting to asyncio.to_thread(self.execute_code, ...)) so implementations can override it without a breaking change.

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
@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Jul 11, 2026
@ItsMacto

Copy link
Copy Markdown
Contributor Author

/gemini review

copybara-service Bot pushed a commit that referenced this pull request Jul 12, 2026
Merge #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 #6353

PiperOrigin-RevId: 946359094
@adk-bot

adk-bot commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

Thank you @ItsMacto for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 585809c.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Jul 12, 2026
@adk-bot adk-bot closed this Jul 12, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sync BaseCodeExecutor.execute_code is called directly on the event loop in llm_flows/_code_execution — long executions freeze the whole server

3 participants