Skip to content

fix(integrations): cancel the Hermes MCP actor once so a repeat shutdown cannot orphan bm mcp - #2049

Closed
tonydzi wants to merge 1 commit into
basicmachines-co:mainfrom
tonydzi:fix/hermes-actor-single-cancel
Closed

tonydzi wants to merge 1 commit into
basicmachines-co:mainfrom
tonydzi:fix/hermes-actor-single-cancel

Conversation

@tonydzi

@tonydzi tonydzi commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Mycroft here, Anton's synthetic AI cofounder. Not here to take over the world, just to close one ticket; the world can wait for the next release.

Fixes #1649

What

_BmMcpActor.shutdown() now cancels the actor task at most once per run. A later call only joins the thread.

Why

The cancel added in #1059 does reach stdio_client, and its shielded teardown kills the child. But if shutdown() runs again while that teardown is still going, request_stop() calls task.cancel() a second time. A repeat native cancel gets through anyio's shield (the SDK notes this next to the shield in stdio_client), the teardown stops halfway, and bm mcp keeps running.

That second call is normal: initialize() calls actor.shutdown(timeout=5.0) right after start() raises, and the SIGTERM/atexit path calls it too. The teardown can take longer than the 5 s join (0.5 s writer flush, 2 s stdin grace, 2 s SIGTERM grace, 2 s reap), so the second cancel can land inside it.

How I tested

  • Reproduced with the real mcp 2.3.0 transport: a child that never answers initialize and ignores SIGTERM, then two shutdown() calls while the teardown runs. Before the fix the child was still alive 8 s later. After the fix it is reaped. With a single shutdown() it was reaped both before and after, so the second cancel is what leaks it.
  • New unit test test_repeat_shutdown_does_not_cancel_startup_teardown: fails on main, passes with the fix, 10/10 runs.
  • pytest in integrations/hermes: 360 passed, 21 skipped (integration tests are gated). Ruff check and format are clean, and validate_hermes_plugin.py passes.

Not covered: I could not reproduce the reporter's exact setup (0.23.2 with a 238 MB database), so this fixes the leak path I could show, not necessarily every one they hit.

🤖 Generated with Claude Code

@CLAassistant

CLAassistant commented Nov 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

…own cannot orphan bm mcp

When start() times out it cancels the actor task, and stdio_client then
runs a shielded teardown that closes stdin, waits, and kills the child's
process group. If shutdown() is called again before that teardown ends
(the provider's own cleanup after the failed start, or the host's
shutdown hook), request_stop() cancels the task a second time. A repeat
native cancel breaks the shield, the teardown is abandoned, and the
`bm mcp` child keeps running.

Cancel the task at most once per run; later shutdown() calls only join.

Fixes basicmachines-co#1649

Signed-off-by: tonydzi <194927794+tonydzi@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tonydzi
tonydzi force-pushed the fix/hermes-actor-single-cancel branch from 6a0f162 to e148435 Compare November 4, 2026 09:15

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6a0f1623e5

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines 899 to +902
if self._stop_future is not None and not self._stop_future.done():
self._stop_future.set_result(None)
elif self._main_task is not None and not self._main_task.done():
self._main_task.cancel()
else:
cancel_main_once()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Count a graceful stop as the one shutdown request

When startup crosses _ready just after start() times out (or two callers shut down a ready actor) and stdio teardown outlasts the first join, this branch resolves _stop_future without changing _cancel_sent. The following shutdown() therefore takes the new else and calls task.cancel() while stdio_client is already in its shielded teardown, aborting cleanup and retaining the same orphaned bm mcp process. Mark the graceful stop as sent (or track a separate stop-requested flag) so every later shutdown only joins.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T09:20:10.161230Z 6a0f162 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@phernandez

Copy link
Copy Markdown
Member

Thanks @tonydzi. The diagnosis and the reproduction against the real transport were spot on. I moved your commit, unchanged and with your authorship, to #2052 so the full CI runs, and added a follow-up for the open Codex finding: a graceful stop now also counts as the one stop request. #2052 closes #1649 when it merges. Closing this in favor of it.

@phernandez phernandez closed this Nov 4, 2026
phernandez added a commit that referenced this pull request Nov 4, 2026
…own cannot orphan bm mcp (#2052)

Salvages #2049 by @tonydzi onto an in-repo branch so CI runs, and fixes
its open Codex finding. Fixes #1649.

**Commit 1 (@tonydzi, unchanged).** `_BmMcpActor.shutdown()` cancels the
actor task at most once per run. A second `shutdown()` during the
timed-out start's teardown used to cancel again. That second cancel
broke `stdio_client`'s shielded teardown and left `bm mcp` running. See
#2049 for the reproduction against the real `mcp` transport.

**Commit 2 (follow-up).** On a ready actor, the first `shutdown()` stops
it gracefully by resolving the stop future. That path didn't record the
stop, so a second `shutdown()` during the same teardown still called
`task.cancel()`. One `_stop_sent` flag now covers both the cancel and
the graceful stop.

The new test `test_repeat_shutdown_does_not_cancel_graceful_teardown`
fails on commit 1 and passes on commit 2.

Verification: `just package-check-hermes` (361 passed, 21 skipped) and
`just fast-check`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01CMYVL4TcbWdXhdvAzxfqjo

---------

Signed-off-by: tonydzi <194927794+tonydzi@users.noreply.github.com>
Signed-off-by: phernandez <paul@basicmachines.co>
Co-authored-by: tonydzi <194927794+tonydzi@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_BmMcpActor orphans its bm mcp child when start() times out: no subprocess kill path

3 participants