Skip to content

Hindsight plugin aiohttp connection leak causes Desktop "session not found" #45676

Closed as not planned

Description

@strugglerrr

What happened

Hermes Desktop becomes unstable after extended use (3-16+ hours), showing random "session not found" errors during normal conversation. The backend process crashes and restarts in a loop.

Root Cause

The Hindsight memory plugin leaks aiohttp ClientSession and TCPConnector objects. Connections are created on every retain/recall/reflect operation but never properly closed on error paths.

Evidence from logs

Before every WebSocket write failure:

ERROR asyncio: Unclosed client session
ERROR asyncio: Unclosed connector
connections: ['deque([(..., 60597.968)])']

Connection ages of 60,597 seconds (16.8 hours) prove connections are never closed — they live from first API call until process death.

Crash chain

aiohttp connections accumulate
  → process becomes sluggish
    → WebSocket write times out
      → Desktop frontend kills backend (SIGHUP)
        → backend restarts
          → 'session not found'
            → loop

Leak Points Found

1. _run_hindsight_operation() line 1031 (PRIMARY)

self._client = None  # old client orphaned, aclose() never called

When a retriable embedded error occurs, the old client is replaced without closing it. Each retry leaks one aiohttp session.

2. HindsightEmbedded.del disabled (line 899)

HindsightEmbedded.__del__ = lambda self: None

Destructor is monkey-patched to no-op, so GC cannot clean up unclosed sessions.

3. shutdown() swallows exceptions (line 1768)

except Exception:
    pass  # close failures silently ignored

If aclose() fails (wrong event loop, timeout), the session leaks silently.

4. No del on HindsightMemoryProvider

When providers are garbage-collected without explicit shutdown(), the underlying aiohttp session is never closed.

Why This Only Affects Desktop Mode

  • TUI: frontend and backend share one process; no WebSocket, so slow backend just causes a pause
  • Desktop: frontend and backend communicate via WebSocket; slow backend causes write timeout → frontend disconnects → 'session not found'

Environment

  • Windows 11, 16GB RAM
  • Hermes Desktop 0.16.0
  • Hindsight Docker (ghcr.io/vectorize-io/hindsight:latest)
  • Python 3.11.15
  • aiohttp 3.13.3

Workaround

Restart Desktop every 3-4 hours to flush accumulated connections.

Suggested Fix

Close old client before replacing in _run_hindsight_operation():

old_client = self._client
self._client = None
if old_client is not None:
    try:
        self._run_sync(old_client.aclose())
    except Exception:
        try:
            old_client.close()
        except Exception:
            pass

Also: add del to HindsightMemoryProvider, stop disabling HindsightEmbedded.del, and log close failures in shutdown() instead of swallowing them.

Activity

  1. added
    type/bugSomething isn't working
    P3Low — cosmetic, nice to have
    comp/pluginsPlugin system and bundled plugins
    tool/memoryMemory tool and memory providers
    on Jun 13, 2026
  2. liuhao1024 commented on Jun 14, 2026

    @liuhao1024

    This is fixed by #45693 — close aiohttp client on retry to prevent connection leak.

  3. GottZ commented on Jul 29, 2026

    @GottZ

    This was generated by AI during triage.

    Summary

    One PR addresses issue #45676. #45693 closes the orphaned aiohttp client before retrying and centralizes explicit shutdown cleanup, directly fixing the primary leak path, but its destructor-related changes conflict with the repository’s documented cross-event-loop cleanup design.

    Related pull requests

    Suggested consolidation

    Merge #45693 after applying its COMMENTED keep_open review: preserve the _close_client() helper, retry-path closure, shutdown reuse, and associated tests, while restoring the embedded destructor suppression and removing the provider-level __del__. There are no duplicate PRs to close.

    Complex graph

    flowchart LR
        classDef open fill:#dbeafe,stroke:#1d4ed8,color:#1e3a8a
        classDef merged fill:#dcfce7,stroke:#15803d,color:#14532d
        classDef closed fill:#e5e7eb,stroke:#6b7280,color:#1f2937
        classDef unverified fill:#f3f4f6,stroke:#9ca3af,color:#374151
        classDef best stroke-width:3px,stroke:#b45309
        classDef target stroke-width:3px,stroke:#4338ca
        I45676(["issue #45676 (open)"])
        P45693["PR #45693 (open)"]
        P45693 -->|best fix| I45676
        class I45676 open
        class I45676 target
        class P45693 open
        class P45693 best
        click I45676 "https://github.com/NousResearch/hermes-agent/issues/45676"
        click P45693 "https://github.com/NousResearch/hermes-agent/pull/45693"
    
    Loading

    Graph: solid arrow = fixes / best fix, dashed arrow = partial or unverified (see edge label); boxed group = PRs duplicating each other; amber border = best fix; indigo border = target; gray node = closed or no verify verdict yet (state tag in the node label).

    Cross-PR triage: Reviewed 1 pull request and 1 issue in this complex. Each diff was read against this issue; Assessment working set: 8 kB of PR diffs, 6 kB of issue/PR text, 1 kB of discussion (2 comments), 2 verify verdicts. verdicts reflect diff content, not PR titles. Part of an automated triage batch.

  4. teknium1 commented on Oct 6, 2026

    @teknium1
    Collaborator

    Thanks for this, @strugglerrr. Hindsight no longer ships inside Hermes: it moved out of core in 4cbf862 and now installs as Vectorize's plugin from the plugin catalog (hermes plugins install hindsight; existing setups migrate automatically). The retry path still looks leaky there: hindsight-integrations/hermes/__init__.py:816 (at d56c4acdf59) drops the old client with self._client = None without closing it first, so the patch from #45693 should carry over almost unchanged. Fixes and features for it now belong in https://github.com/vectorize-io/hindsight (the plugin lives under hindsight-integrations/hermes) — please open it there. Closing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low — cosmetic, nice to havearea/sessionsSession lifecycle, resume, persistence, historycomp/pluginsPlugin system and bundled pluginstool/memoryMemory tool and memory providerstype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions