Skip to content

[Bug]: Home Assistant tool still uses per-call asyncio.run() path inside running event loops #13586

@stepanov1975

Description

@stepanov1975

Bug Description

tools/homeassistant_tool.py reuses persistent event loops for some sync call paths, but _run_async() still falls back to a disposable-thread + asyncio.run(coro) path when called from inside an already-running event loop.

In long-running Hermes processes, that per-call create-and-destroy loop behavior can leave aiohttp cleanup tied to dead loops and risks Unclosed client session / Unclosed connector warnings.

Steps to Reproduce

  1. Start from current main.
  2. Call tools/homeassistant_tool.py:_run_async() from within an already-running event loop.
  3. Exercise a Home Assistant tool path that creates and cleans up an async HTTP client/session.
  4. Observe that this branch still creates a fresh event loop via asyncio.run() for each call instead of using a persistent loop.

Expected Behavior

When _run_async() is called from inside a running event loop, the Home Assistant tool should use a persistent async bridge loop so async client cleanup stays bound to a live event loop.

Actual Behavior

The async-context branch creates a disposable thread and runs asyncio.run(coro) per invocation, which creates and immediately closes a fresh event loop for that call.

Environment

Error Output

Observed risk/symptom in long-running processes:

ERROR asyncio: Unclosed client session
ERROR asyncio: Unclosed connector

Relevant problematic pattern:

if loop and loop.is_running():
    import concurrent.futures
    with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
        future = pool.submit(asyncio.run, coro)
        return future.result(timeout=30)

Additional Context

This issue is narrowly scoped to tools/homeassistant_tool.py.

It is related to async resource lifecycle management and the same general class of sync-to-async bridging problems seen elsewhere in Hermes, but I did not find an existing open issue that cleanly covers this exact Home Assistant tool path.

A proposed fix is in PR #13422:
#13422

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp/toolsTool registry, model_tools, toolsetstool/mcpMCP client and OAuthtype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions