Skip to content

fix(memory): close MCP backend on shutdown - #2905

Merged
chopratejas merged 1 commit into
headroomlabs-ai:mainfrom
abhinavkr26104:fix/2898-memory-mcp-cleanup
Aug 11, 2026
Merged

chopratejas merged 1 commit into
headroomlabs-ai:mainfrom
abhinavkr26104:fix/2898-memory-mcp-cleanup

Conversation

@abhinavkr26104

Copy link
Copy Markdown
Contributor

Description

Closes the initialized LocalBackend and cancels in-flight initialization whenever the memory MCP stdio transport exits.

Fixes #2898

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Performance improvement
  • Code refactoring (no functional changes)

Changes Made

  • Added an explicit server cleanup callback that cancels and awaits pending backend initialization.
  • Closes an initialized backend exactly once and clears the backend/task references.
  • Runs cleanup in _run() through a finally block after the stdio transport exits, including transport errors.
  • Added regression coverage for initialized cleanup, pending initialization cancellation, idempotence, and _run() shutdown behavior.

Testing

  • Unit tests pass (pytest)
  • Linting passes (ruff check .)
  • Type checking passes (mypy headroom)
  • New tests added for new functionality
  • Manual testing performed

Test Output

python -m pytest -q tests/test_memory/test_mcp_server.py
15 passed, 20 warnings

ruff check .
All checks passed!

ruff format --check .
1382 files already formatted

python -m mypy headroom
Success: no issues found in 515 source files

python -m pytest -q
Collected 8881 items / 174 errors / 18 skipped.
Interrupted during collection because this Windows environment lacks the compiled headroom._core extension.

Real Behavior Proof

  • Environment: Windows, Python 3.12, async MCP server lifecycle test with the real create_memory_server() closure and an embedded server transport stub.
  • Exact command / steps: Ran python -m pytest -q tests/test_memory/test_mcp_server.py; the regression tests initialized a backend through the server's registered tool lifecycle, returned the stdio transport, and invoked the cleanup callback from _run()'s finally path.
  • Observed result: 15 tests passed. Initialized backends were closed once, pending initialization was cancelled and awaited, and transport exit invoked cleanup even when the server run returned.
  • Who maintains it: Headroom Labs maintains this active upstream repository and memory MCP server.
  • Install surface: No dependencies or install behavior changed. The fix uses existing asyncio lifecycle handling and LocalBackend.close(); no native code or runtime network access is introduced.
  • Not tested: The complete repository suite could not run past collection because this Windows environment lacks the compiled headroom._core extension.

Review Readiness

  • I have performed a self-review
  • This PR is ready for human review

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (full suite blocked by missing native extension; targeted tests pass)
  • I did not edit CHANGELOG.md - it is generated by release-please from my Conventional Commit PR title.

Screenshots (if applicable)

Not applicable.

Additional Notes

Cleanup is attached to each created memory MCP server and is idempotent, so embedded callers can invoke the same lifecycle callback safely if needed.

@github-actions

Copy link
Copy Markdown
Contributor

PR governance

This PR does not yet satisfy the required template fields:

  • Missing required section Description.

Please update the PR body, or move the PR back to draft while it is still in progress.

@github-actions github-actions Bot added the status: needs author action Pull request body or readiness checklist still needs author updates label Aug 10, 2026

@JerrettDavis JerrettDavis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup is idempotent and serialized, cancels and awaits an in-flight initializer, and relies on _init_backend’s existing cancellation path to close partially constructed resources exactly once. A completed backend is detached before close, and _run invokes cleanup in finally across normal and exceptional stdio exits. The actual MCP Server supports the attached cleanup callback; focused tests cover initialized, initializing, and run-exit paths. Broader CI has not run.

@codecov-commenter

codecov-commenter commented Aug 11, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 75.00000% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
headroom/memory/mcp_server.py 75.00% 3 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@chopratejas chopratejas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintainer review complete: cleanup is serialized and idempotent, cancels and awaits in-flight initialization, closes completed or partially initialized backends exactly once, and runs from the transport finally path. Merge after full CI.

@chopratejas
chopratejas merged commit 4bd8ecd into headroomlabs-ai:main Aug 11, 2026
50 of 51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: needs author action Pull request body or readiness checklist still needs author updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Memory MCP stdio server never closes its initialized backend

4 participants