Python: run sync tools off the event loop#5773
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the Python FunctionTool.invoke async execution path so that synchronous tools run in a worker thread (avoiding event-loop stalls), while async tools continue to run on the event loop. It also adds a regression test to ensure a blocking sync tool no longer prevents other coroutines from running (fixing #5741).
Changes:
- Add an internal async helper to route async tools to the event loop and sync tools to
asyncio.to_thread(). - Update
FunctionTool.invoketo use the helper in both the observability-enabled and disabled code paths. - Add an asyncio/threading regression test to confirm sync tool invocation does not block the event loop.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
python/packages/core/agent_framework/_tools.py |
Offloads synchronous tool execution to a worker thread during async invocation via a new helper. |
python/packages/core/tests/core/test_tools.py |
Adds a regression test validating that sync tools no longer block other event-loop tasks. |
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||||||||||||
2d983ec to
37330b7
Compare
|
Rebased this branch onto current Validation: |
37330b7 to
7a0e06e
Compare
|
@he-yufeng please check the failing tests |
Head branch was pushed to by a user without write access
7a0e06e to
42ff4ae
Compare
|
Rebased this branch onto current Validation on the rebased head: |
|
@he-yufeng Unit tests are still failing. Please have another look, thanks! |
42ff4ae to
8d4af1c
Compare
|
Thanks for the heads-up. The failing Python tests were coming from the background-agent tools: I updated the branch so normal sync tools still run off the event loop by default, while these two event-loop-dependent harness tools opt into running on the current loop. I also rebased the branch onto current Validated locally: |
|
Rebased this branch onto current Validation on Windows: The targeted |
8d4af1c to
e30c431
Compare
|
FAILED: pyright in packages/core |
|
Pushed a small follow-up for the package-check failure. Root cause: package-wide pyright reports the dynamic Validation run locally:
The Windows full-test jobs also reported a |
Summary
FunctionTool.invokepathFixes #5741.
To verify
uv run pytest tests/core/test_tools.py -quv run ruff check agent_framework tests/core/test_tools.pyuv run ruff format --check agent_framework tests/core/test_tools.pyuv run pyrightuv run mypy --config-file pyproject.toml agent_frameworkpython -m py_compile agent_framework\_tools.py tests\core\test_tools.py