fix(plugins): doctor temp home can no longer be stranded by a failed staging copy

Follow-up to the salvaged manifest guard (#90859): the doctor's temporary
HERMES_HOME now enters the ExitStack before the staging copytree, so
ENOSPC / KeyboardInterrupt / any exception during staging deterministically
removes the hermes-plugin-doctor-* directory instead of relying on the
TemporaryDirectory GC finalizer (which cannot run while the traceback pins
the frame). Regression test proven via sabotage run against the old code.
This commit is contained in:
Teknium
2026-08-31 07:44:45 -07:00
parent a00ae1d519
commit ca9952cbb3
3 changed files with 83 additions and 29 deletions
@@ -0,0 +1 @@
DevKJ
+36 -27
View File
@@ -40,34 +40,44 @@ def _doctor_runtime(plugin_path: Path):
test framework. Registration code executes under a temporary HERMES_HOME
with outbound socket connects blocked.
"""
temporary_home = tempfile.TemporaryDirectory(prefix="hermes-plugin-doctor-")
stack = ExitStack()
home = Path(temporary_home.name)
bundled = home / "bundled-plugins"
plugins_root = home / "plugins"
bundled.mkdir(parents=True)
plugins_root.mkdir(parents=True)
copied = plugins_root / plugin_path.name
shutil.copytree(
plugin_path,
copied,
ignore=shutil.ignore_patterns(".git", "__pycache__", ".pytest_cache", "*.pyc"),
)
stack.enter_context(
patch.dict(
os.environ,
{
"HERMES_HOME": str(home),
"HERMES_BUNDLED_PLUGINS": str(bundled),
"HERMES_ENABLE_PROJECT_PLUGINS": "0",
},
clear=False,
try:
# The temp dir enters the stack FIRST so any failure below — including
# ENOSPC or KeyboardInterrupt inside copytree — deterministically
# removes it instead of stranding a hermes-plugin-doctor-* directory
# until interpreter GC (or never, on a hard exit).
temporary_home = stack.enter_context(
tempfile.TemporaryDirectory(prefix="hermes-plugin-doctor-")
)
)
stack.enter_context(patch.object(socket, "create_connection", _deny_network))
stack.enter_context(patch.object(socket.socket, "connect", _deny_network))
stack.enter_context(patch.object(socket.socket, "connect_ex", _deny_network))
home = Path(temporary_home)
bundled = home / "bundled-plugins"
plugins_root = home / "plugins"
bundled.mkdir(parents=True)
plugins_root.mkdir(parents=True)
copied = plugins_root / plugin_path.name
shutil.copytree(
plugin_path,
copied,
ignore=shutil.ignore_patterns(".git", "__pycache__", ".pytest_cache", "*.pyc"),
)
stack.enter_context(
patch.dict(
os.environ,
{
"HERMES_HOME": str(home),
"HERMES_BUNDLED_PLUGINS": str(bundled),
"HERMES_ENABLE_PROJECT_PLUGINS": "0",
},
clear=False,
)
)
stack.enter_context(patch.object(socket, "create_connection", _deny_network))
stack.enter_context(patch.object(socket.socket, "connect", _deny_network))
stack.enter_context(patch.object(socket.socket, "connect_ex", _deny_network))
except BaseException:
stack.close()
raise
from hermes_cli.plugins import PluginManager
from tools.registry import registry
@@ -130,7 +140,6 @@ def _doctor_runtime(plugin_path: Path):
):
sys.modules.pop(name, None)
stack.close()
temporary_home.cleanup()
@dataclass(frozen=True)
+46 -2
View File
@@ -1,6 +1,8 @@
from __future__ import annotations
import argparse
import pytest
from pathlib import Path
from hermes_cli.subcommands.plugins import build_plugins_parser
@@ -138,8 +140,6 @@ def test_doctor_blocks_live_network(tmp_path: Path) -> None:
def test_resolve_rejects_directory_without_manifest(tmp_path: Path) -> None:
"""A non-plugin directory must not resolve — Doctor copies what it resolves."""
import pytest
from hermes_cli.plugin_dev import resolve_plugin_path
not_a_plugin = tmp_path / "home"
@@ -215,3 +215,47 @@ def test_resolve_prefers_installed_id_over_unrelated_local_dir(
monkeypatch.setattr(plugin_dev, "get_hermes_home", lambda: hermes_home)
assert plugin_dev.resolve_plugin_path("sample") == installed.resolve()
def test_doctor_removes_temp_home_when_staging_copy_fails(
tmp_path: Path, monkeypatch
) -> None:
"""A copy failure (e.g. ENOSPC) must not strand a hermes-plugin-doctor-* dir."""
import errno
import shutil
import tempfile
from hermes_cli import plugin_dev
plugin = tmp_path / "sample"
plugin.mkdir()
(plugin / "plugin.yaml").write_text("name: sample\n", encoding="utf-8")
(plugin / "__init__.py").write_text(
"def register(ctx):\n pass\n", encoding="utf-8"
)
scratch = tmp_path / "scratch-tmp"
scratch.mkdir()
monkeypatch.setattr(tempfile, "tempdir", str(scratch))
def _enospc(*_args, **_kwargs):
raise OSError(errno.ENOSPC, "No space left on device")
monkeypatch.setattr(shutil, "copytree", _enospc)
caught: OSError | None = None
try:
with plugin_dev._doctor_runtime(plugin):
pass # pragma: no cover - staging fails before yield
except OSError as exc:
caught = exc
assert caught is not None and caught.errno == errno.ENOSPC
# Check for leftovers WHILE the exception (and its traceback frames) is
# still referenced: the old code relied on TemporaryDirectory's GC
# finalizer, which cannot run while the traceback pins the frame — the
# exact window where a stranded hermes-plugin-doctor-* dir was observed.
leftovers = list(scratch.glob("hermes-plugin-doctor-*"))
assert leftovers == [], f"stranded doctor temp dirs: {leftovers}"