Skip to content

fix(run_agent): shut down background review memory providers#15289

Closed
mrhwick wants to merge 4 commits into
NousResearch:mainfrom
mrhwick:fix/background-review-memory-shutdown
Closed

fix(run_agent): shut down background review memory providers#15289
mrhwick wants to merge 4 commits into
NousResearch:mainfrom
mrhwick:fix/background-review-memory-shutdown

Conversation

@mrhwick

@mrhwick mrhwick commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

This fixes a cleanup gap in the temporary background review agent spawned by _spawn_background_review() in run_agent.py.

That review agent can initialize memory providers such as Hindsight, but its teardown path only called close(). AIAgent.close() does not shut down memory providers, so Hindsight-owned aiohttp client sessions could survive until GC or process exit and emit Unclosed client session / Unclosed connector warnings.

This change explicitly calls shutdown_memory_provider() before close() for the background review agent, which matches the existing explicit session-boundary cleanup pattern used elsewhere in the codebase. It also adds a regression test that verifies the cleanup order.

Related Issue

Fixes #

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • Update run_agent.py so the temporary review agent in _spawn_background_review() calls shutdown_memory_provider() before close().
  • Add tests/run_agent/test_background_review.py to verify the background review teardown path shuts down the memory provider before closing the agent.
  • Keep the fix narrowly scoped to the temporary review-agent path rather than changing the broader semantics of AIAgent.close().

How to Test

  1. Run scripts/run_tests.sh tests/run_agent/test_background_review.py.
  2. Configure Hermes with Hindsight enabled and exercise a path that spawns the background memory/skill review agent.
  3. Confirm the review-agent teardown no longer leaves behind aiohttp warnings such as Unclosed client session or Unclosed connector.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 15.3

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

mrhwick added 4 commits April 24, 2026 13:40
Temporary background review agents can initialize Hindsight-backed memory clients, but close() alone skips provider teardown. Shut the memory provider down before closing so aiohttp sessions do not leak at process exit.

Made-with: Cursor
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder tool/memory Memory tool and memory providers labels Apr 24, 2026
@trevorgordon981

Copy link
Copy Markdown

Review: Fix background review memory provider shutdown\n\n✅ Critical bug fix - well done!\n\nThe problem:\n- Background review agents could leave client sessions open\n- Resulted in / warnings\n- Only happened with memory providers like Hindsight that manage their own network clients\n\nThe fix:\n- Explicitly call before \n- Matches the existing session-boundary cleanup pattern\n- Narrowly scoped to just the temporary review agent path\n\nTest coverage:\n- Perfect regression test verifying cleanup order\n- Confirms is called before \n\nQuestion: Should we consider updating documentation to explicitly state it doesn't shut down memory providers, so future contributors don't make the same mistake?\n\nThis fixes a real resource leak that users were seeing. Approved! 🐛🔧

@trevorgordon981

Copy link
Copy Markdown

Critical fix for resource leak. Explicit shutdown_memory_provider call before close is the right pattern. Approved!

@teknium1

Copy link
Copy Markdown
Contributor

Merged via salvage PR #16204. Your three commits were rebase-merged onto current main with your authorship preserved in git log (2d86e97, 36e352a, aa7b5ac). We added one follow-up commit (45bfcb9) extending the test's bare-agent helper so it covers session_id, _credential_pool, and status_callback — live-runtime attrs the review fork started touching after #16099 landed. Thanks for the fix.

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

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P2 Medium — degraded but workaround exists tool/memory Memory tool and memory providers type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants