Skip to content

Offload sync session tools from the event loop - #1853

Open
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/session-sync-tool-offload
Open

Offload sync session tools from the event loop#1853
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/session-sync-tool-offload

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Prevent synchronous tools accepted by SessionToolRunner from blocking the async event loop.

The managed-agents session runner explicitly accepts both:

  • BetaRunnableTool (synchronous)
  • BetaAsyncRunnableTool (asynchronous)

through BetaAnyRunnableTool.

Its shared dispatcher currently calls every tool synchronously first:

result = tool.call(input)

and only afterward checks whether the result is awaitable.

For a synchronous tool, the entire tool body therefore runs on the event-loop thread. A blocking filesystem, subprocess, network, or user-defined sync tool can stall the session event stream and other async coordination.

Fix

Dispatch based on the callable shape before invoking it.

  • native async tools remain on the event loop;
  • synchronous tools execute through anyio.to_thread.run_sync;
  • synchronous wrappers returning an awaitable remain supported.

Regression coverage

Adds deterministic tests proving:

  • synchronous tools execute on a worker thread;
  • native async tools remain on the event-loop thread;
  • sync wrappers returning awaitables still work correctly.

@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner August 17, 2026 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant