Files
hermes-agent/tests/hermes_cli/test_plugin_module_eviction_race.py
unsupportedpastels 230fa9564b test(plugins): reproduce the module-eviction mapping race deterministically
Seven platform plugins failed at one Windows gateway startup with
"dictionary changed size during iteration" (#139134). Every directory-plugin
load starts with _evict_modules, which walked the live sys.modules; another
thread adding a module during that walk raises exactly this error. That is a
credible cause of the report (identical error text, a concurrent telegram
import timeout in the same boot), but the report has no traceback naming
_evict_modules, so it is a diagnosis, not a confirmed attribution.

The fix is NaMinhyeok's #105519, salvaged unchanged in the three commits
before this one. This adds a regression test that reproduces a concurrent
mutation of the module mapping during eviction deterministically: a private
stand-in for sys.modules gains an entry from a second thread at a fixed point
of the walk. It fails on every run on the base (RuntimeError at
plugins_loader.py:157) and passes with the snapshot, without relying on GIL
switch timing. It performs no real import and does not reproduce gateway
startup or concurrent profile preparation.

Other open PRs proposing the same snapshot: #123936, #125854, #127620,
#132367, #132434, #135987, #138203.

Refs #139134
Refs #134889 (PLG-10)
Refs #139523
2026-10-16 12:47:18 +00:00

47 lines
2.0 KiB
Python

"""A module mapping that grows while ``_evict_modules`` walks it must not fail the eviction (refs #139134).
``_evict_modules`` runs at the start of every directory-plugin import, and other threads can add to
``sys.modules`` meanwhile; iterating the live mapping raises "dictionary changed size during
iteration". This reproduces that concurrent mutation deterministically: a private stand-in for
``sys.modules`` gains an entry from a second thread at a fixed point of the walk. It performs no real
import and does not reproduce gateway startup or concurrent profile preparation, so it does not by
itself show this race caused the failures reported in #139134.
"""
from __future__ import annotations
import threading
import types
from hermes_cli import plugins_loader
def test_evict_modules_survives_a_concurrent_mapping_insert(monkeypatch):
modules: dict[str, types.ModuleType] = {}
handed_off = threading.Event()
def _insert_elsewhere() -> None:
modules["race_probe_sdk.client"] = types.ModuleType("race_probe_sdk.client")
class _SwitchPoint(str):
"""A key whose prefix test runs a thread that inserts into the mapping mid-walk, deterministically."""
def startswith(self, *args):
if not handed_off.is_set():
handed_off.set()
worker = threading.Thread(target=_insert_elsewhere)
worker.start()
worker.join()
return str.startswith(self, *args)
for name in ("hermes_plugins.discord_platform", "hermes_plugins.discord_platform.adapter"):
modules[name] = types.ModuleType(name)
modules[_SwitchPoint("unrelated")] = types.ModuleType("unrelated")
modules["hermes_plugins.discord_platform.tools"] = types.ModuleType("hermes_plugins.discord_platform.tools")
monkeypatch.setattr(plugins_loader, "sys", types.SimpleNamespace(modules=modules))
plugins_loader._evict_modules("hermes_plugins.discord_platform")
assert handed_off.is_set()
assert sorted(modules) == ["race_probe_sdk.client", "unrelated"]