Skip to content

The agent that grows with you

fix(plugins): snapshot sys.modules keys before evicting plugin submodules

NaMinhyeok1f6e0ddb524d
diff --git a/hermes_cli/plugins_loader.py b/hermes_cli/plugins_loader.py
index c53c03a08b1..39da77c5635 100644
--- a/hermes_cli/plugins_loader.py
+++ b/hermes_cli/plugins_loader.py
@@ -152,10 +152,16 @@ def run_with_load_deadline(plugin_key: str, ctx: PluginContext, fn: Callable[[],
def _evict_modules(module_name: str) -> None:
- """Drop ``module_name`` and every ``module_name.*`` submodule from ``sys.modules``."""
+ """Drop ``module_name`` and every ``module_name.*`` submodule from ``sys.modules``.
+
+ ``sys.modules`` is process-global, so any concurrent import — another profile's plugin
+ load, a lazy import on a worker thread — mutates it while this runs. Snapshot the keys
+ with ``list()`` (one atomic C-level copy) instead of iterating the live mapping, and drop
+ with ``pop`` so a key another evictor already removed is not a ``KeyError``.
+ """
prefix = f"{module_name}."
- for name in [n for n in sys.modules if n == module_name or n.startswith(prefix)]:
- del sys.modules[name]
+ for name in [n for n in list(sys.modules) if n == module_name or n.startswith(prefix)]:
+ sys.modules.pop(name, None)
def _serialized_replacement(method):
diff --git a/tests/hermes_cli/test_plugin_loader_module_eviction.py b/tests/hermes_cli/test_plugin_loader_module_eviction.py
new file mode 100644
index 00000000000..d97868995ed
--- /dev/null
+++ b/tests/hermes_cli/test_plugin_loader_module_eviction.py
@@ -0,0 +1,91 @@
+"""Regression: ``_evict_modules`` must survive concurrent mutation of ``sys.modules``."""
+
+from __future__ import annotations
+
+import sys
+import threading
+import types
+
+import pytest
+
+from hermes_cli import plugins_loader
+
+# Enough entries that the filtering pass spans several GIL switches.
+_MODULE_COUNT = 5000
+_EVICTIONS = 300
+# Batched so the mapping's *size* genuinely differs across the eviction's iteration window.
+_CHURN_BATCH = 50
+
+
+def isolated_modules(monkeypatch: pytest.MonkeyPatch) -> dict:
+ """Point the loader at a private ``sys.modules`` stand-in.
+
+ A real dict, because the fix rests on ``list(dict)`` being one atomic C-level copy —
+ a Python-level mapping stub would not exercise that property.
+ """
+ modules: dict[str, object] = {
+ f"unrelated_{i}": types.ModuleType(f"unrelated_{i}") for i in range(_MODULE_COUNT)
+ }
+ monkeypatch.setattr(plugins_loader, "sys", types.SimpleNamespace(modules=modules))
+ return modules
+
+
+def test_evict_modules_drops_module_and_submodules(isolated_modules: dict) -> None:
+ for name in ("hermes_plugins.aop", "hermes_plugins.aop.client", "hermes_plugins.aop_other"):
+ isolated_modules[name] = types.ModuleType(name)
+
+ plugins_loader._evict_modules("hermes_plugins.aop")
+
+ assert "hermes_plugins.aop" not in isolated_modules
+ assert "hermes_plugins.aop.client" not in isolated_modules
+ # A sibling sharing the prefix without the dot boundary must survive.
+ assert "hermes_plugins.aop_other" in isolated_modules
+
+
+def test_evict_modules_tolerates_concurrent_imports(isolated_modules: dict) -> None:
+ """A concurrent import must not abort the plugin load.
+
+ Iterating the live mapping raised ``RuntimeError: dictionary changed size during
+ iteration``; ``PluginLoader._load`` catches it and degrades to
+ ``Failed to load plugin '<name>': ...``, so none of that plugin's tools ever register
+ and every later lookup reports the tool as unknown.
+ """
+ stop = threading.Event()
+ failures: list[BaseException] = []
+
+ def churn() -> None:
+ i = 0
+ while not stop.is_set():
+ batch = [f"late_import_{i}_{j}" for j in range(_CHURN_BATCH)]
+ for key in batch:
+ isolated_modules[key] = None
+ for key in batch:
+ isolated_modules.pop(key, None)
+ i += 1
+
+ def evict() -> None:
+ try:
+ for i in range(_EVICTIONS):
+ name = f"hermes_plugins.victim_{i}"
+ isolated_modules[name] = None
+ isolated_modules[f"{name}.sub"] = None
+ plugins_loader._evict_modules(name)
+ except BaseException as exc: # noqa: BLE001 - recorded for the assertion below
+ failures.append(exc)
+
+ previous_interval = sys.getswitchinterval()
+ sys.setswitchinterval(1e-6) # force frequent handoffs so the race is deterministic
+ churner = threading.Thread(target=churn, daemon=True)
+ churner.start()
+ try:
+ evictor = threading.Thread(target=evict)
+ evictor.start()
+ evictor.join(timeout=60)
+ assert not evictor.is_alive(), "eviction thread hung"
+ finally:
+ stop.set()
+ churner.join(timeout=10)
+ sys.setswitchinterval(previous_interval)
+
+ assert not failures, f"eviction raised under concurrent import: {failures[0]!r}"