Skip to content

[Bug]: Hindsight still leaks aiohttp ClientSession/connector after fix #4762 #11923

@AgnoyZ

Description

@AgnoyZ

Bug Description

The Hindsight memory integration still emits aiohttp resource-leak warnings in a long-running Hermes gateway process even after the earlier Hindsight fix from PR #4762 (fix(hindsight): correct client constructor, add configurable base_url, and fix sequential tool dispatch).

The previous fix improved the plugin, but it did not eliminate the underlying Unclosed client session / Unclosed connector problem.

Symptoms

Observed repeatedly in gateway logs:

ERROR asyncio: Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x...>
ERROR asyncio: Unclosed connector
connector: <aiohttp.connector.TCPConnector object at 0x...>

Why this looks unresolved

The Hindsight plugin now has shutdown cleanup logic that calls self._client.aclose() during provider teardown, but the warnings still occur during normal runtime in the gateway logs, not only at final process shutdown.

Relevant code path in plugins/memory/hindsight/__init__.py:

if self._client is not None:
    try:
        if self._mode == "local_embedded":
            try:
                self._client.close()
            except RuntimeError:
                pass
        else:
            _run_sync(self._client.aclose())
    except Exception:
        pass
    self._client = None

That suggests one of these is still happening:

  1. A client/session is created outside the lifecycle covered by provider shutdown.
  2. The async client owns an aiohttp session that is not being fully closed in all paths.
  3. The shared background loop / cleanup timing still leaves transport resources orphaned.
  4. The leak happens during repeated memory operations in a long-running process, so shutdown cleanup is too late to prevent runtime warnings.

Steps to Reproduce

  1. Configure Hermes to use the Hindsight memory provider.
  2. Run the gateway as a long-lived process.
  3. Trigger memory activity (for example: normal chat flow with memory prefetch, or direct hindsight_recall / hindsight_reflect / hindsight_retain usage).
  4. Check gateway logs.

Expected Behavior

No aiohttp resource leak warnings should appear during normal operation.

Actual Behavior

Gateway logs still show:

ERROR asyncio: Unclosed client session
ERROR asyncio: Unclosed connector

Sanitized Evidence

Example sanitized log sequence:

INFO plugins.memory.hindsight: Hindsight initialized: mode=cloud, api_url=http://YOUR_HINDSIGHT_HOST:8888, bank=main_hermes, budget=mid, memory_mode=hybrid, prefetch_method=recall, client=0.5.0
ERROR asyncio: Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x...>
ERROR asyncio: Unclosed connector
connections: ['deque([(<aiohttp.client_proto.ResponseHandler object at 0x...>, ...)])']
connector: <aiohttp.connector.TCPConnector object at 0x...>

This was observed multiple times across separate runtime windows, not as a one-off shutdown-only warning.

Environment

  • Hermes Agent: current local deployment (exact upstream commit not verified before filing)
  • Memory provider: Hindsight
  • Hindsight client version reported by logs: 0.5.0
  • Hindsight mode: cloud / external server
  • Python runtime: long-running gateway process

Additional Context

This looks like a follow-up / regression gap after PR #4762 rather than a brand-new issue. The previous repair addressed constructor/base_url/dispatch problems, but not the persistent aiohttp session cleanup problem.

It may be worth auditing:

  • all Hindsight client creation sites,
  • whether one Hindsight instance internally owns multiple async sessions,
  • whether cleanup happens on the same event loop that created the underlying resources,
  • whether per-call temporary sessions survive even when the top-level client is reused.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/pluginsPlugin system and bundled pluginstool/memoryMemory tool and memory providerstype/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