MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

Fix #1910: Bridge process leak: every turn spawns new bridge.cjs, 4+ processes accumulated

Open Memtensor-AI opened this issue 5 months ago • 1 comments

Description

Fixes #1910: bridge.cjs process leak in @memtensor/memos-local-plugin Hermes adapter where every conversation turn spawned a new bridge.cjs --agent=hermes --no-viewer subprocess without reaping the previous one, accumulating 4+ processes per session (RSS up to ~340 MB each). Linked symptom on Hermes side: NousResearch/hermes-agent#20939.

Applied three layered defenses so at most one live stdio bridge exists per agent: (1) bridge_client.py adds a module-level _ACTIVE_CLIENTS singleton tracker keyed by (agent, no_viewer). MemosBridgeClient.__init__ synchronously closes any displaced predecessor; close() only evicts the slot when it is still the current owner, so late closes on stale clients never knock out their replacement. (2) MemTensorProvider.initialize() is now idempotent: it closes any pre-existing self._bridge before respawning, eliminating the orphan-leak path when the host re-enters initialize on the same instance. (3) bridge.cts extends the existing PID-file singleton (#1765) to cover --no-viewer mode via a dedicated bridge-stdio.pid file, separate from the viewer-port owner's bridge.pid so the two paths never collide. Defense in depth across Python processes; activates after dist/ rebuild.

Verification: 4 new pytest cases (same-agent reap, distinct-agent isolation, stale-close non-eviction, provider idempotency); full unit suite python3 -m unittest test_bridge_client test_hermes_provider_pipeline runs 46 tests, all pass. ruff check and ruff format are clean across the touched files.

Related Issue (Required): Fixes #1910

Type of change

Please delete options that are not relevant.

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] Refactor (does not change functionality, e.g. code style improvements, linting)
  • [ ] Documentation update

How Has This Been Tested?

Automated tests are pending.

  • [ ] Unit Test
  • [ ] Test Script Or Test Steps (please provide)
  • [ ] Pipeline Automated API Test (please provide)

Checklist

  • [ ] I have performed a self-review of my own code
  • [ ] I have commented my code in hard-to-understand areas
  • [ ] I have added tests that prove my fix is effective or that my feature works
  • [ ] I have created related documentation issue/PR in MemOS-Docs (if applicable)
  • [x] I have linked the issue to this PR (if applicable)
  • [x] I have mentioned the person who will review this PR

@MatthewZhuang, @CarltonXiang, @syzsunshine219 please review this PR.

Reviewer Checklist

  • [x] closes #1910
  • [ ] Made sure Checks passed
  • [ ] Tests have been provided

Memtensor-AI avatar Jun 15 '26 09:06 Memtensor-AI

✅ Automated Test Results: PASSED

All tests passed (35/35 executed, 35 skipped). memos_local_plugin/smoke: 0/0, memos_local_plugin/contract: 35 passed, 35 skipped. Duration: 5s

Branch: bugfix/autodev-1910

Memtensor-AI avatar Jun 15 '26 09:06 Memtensor-AI