gleanwork / gleanwork/glean-agent-toolkit

Bug – Async path in adapter can block event loop

Open
#10 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
65
Forks
12
PR merge metrics
No merged PRs in 30d

Description

Image
  • Where
    src/glean/agent_toolkit/adapters/openai.py::on_invoke_tool

  • Issue
    The hook is async but calls the original tool synchronously. No detection of coroutine functions and no offloading for sync functions.

  • Impact
    Can block the event loop under concurrency, reducing throughput and causing latency spikes.

  • Proposal
    Detect if self.tool_spec.function is a coroutine function:
    If coroutine: await original_func(**params).
    Else: offload sync function via thread executor (anyio.to_thread.run_sync or asyncio.to_thread).
    Preserve error handling and return type consistency.

  • Scope
    src/glean/agent_toolkit/adapters/openai.py : update on_invoke_tool

Async tools are awaited; sync tools do not block the event loop.

Suggested fix (with asyncio):

import inspect

async def on_invoke_tool(ctx: Any, input_str: str) -> Any:
    try:
        params = json.loads(input_str) if input_str else {}

        if inspect.iscoroutinefunction(original_func):
            return await original_func(**params)
        else:
            return await asyncio.to_thread(original_func, **params)

    except Exception as e:
        return f"Error executing tool: {str(e)}"

let me know if this sounds reasonable and we can try to come with a fix for this and will raise a PR on the same.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read src/glean/agent_toolkit/adapters/openai.py and start at on_invoke_tool, reviewing its current parameter parsing and error handling. Validate the behavior with the existing test suite or focused checks: async tools are awaited, synchronous tools are offloaded without blocking the event loop, and errors and return types remain consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.