Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 44 additions & 38 deletions providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import importlib.util
import logging
import sys
import threading
from pathlib import Path

from providers.base import OMIT_TEMPERATURE, ProviderProfile # noqa: F401
Expand All @@ -43,6 +44,7 @@
_REGISTRY: dict[str, ProviderProfile] = {}
_ALIASES: dict[str, str] = {}
_discovered = False
_discover_lock = threading.Lock()

# Repo-root ``plugins/model-providers/`` — populated at discovery time.
_BUNDLED_PLUGINS_DIR = (
Expand Down Expand Up @@ -151,41 +153,45 @@ def _discover_providers() -> None:
global _discovered
if _discovered:
return
_discovered = True

# 1. Bundled plugins — shipped with hermes-agent.
if _BUNDLED_PLUGINS_DIR.is_dir():
for child in sorted(_BUNDLED_PLUGINS_DIR.iterdir()):
if not child.is_dir() or child.name.startswith(("_", ".")):
continue
_import_plugin_dir(child, "bundled")

# 2. User plugins — under $HERMES_HOME/plugins/model-providers/<name>/.
# These can override any bundled profile of the same name (last-writer-wins
# in register_provider()).
user_dir = _user_plugins_dir()
if user_dir is not None:
for child in sorted(user_dir.iterdir()):
if not child.is_dir() or child.name.startswith(("_", ".")):
continue
_import_plugin_dir(child, "user")

# 3. Legacy single-file profiles at providers/<name>.py. Kept for
# back-compat — if someone drops a ``providers/foo.py`` into an
# editable install, it still works without the plugin layout.
try:
import pkgutil

import providers as _pkg

for _importer, modname, _ispkg in pkgutil.iter_modules(_pkg.__path__):
if modname.startswith("_") or modname == "base":
continue
try:
importlib.import_module(f"providers.{modname}")
except ImportError as exc:
logger.warning(
"Failed to import legacy provider module %s: %s", modname, exc
)
except Exception:
pass
with _discover_lock:
if _discovered:
return

# 1. Bundled plugins — shipped with hermes-agent.
if _BUNDLED_PLUGINS_DIR.is_dir():
for child in sorted(_BUNDLED_PLUGINS_DIR.iterdir()):
if not child.is_dir() or child.name.startswith(("_", ".")):
continue
_import_plugin_dir(child, "bundled")

# 2. User plugins — under $HERMES_HOME/plugins/model-providers/<name>/.
# These can override any bundled profile of the same name (last-writer-wins
# in register_provider()).
user_dir = _user_plugins_dir()
if user_dir is not None:
for child in sorted(user_dir.iterdir()):
if not child.is_dir() or child.name.startswith(("_", ".")):
continue
_import_plugin_dir(child, "user")

# 3. Legacy single-file profiles at providers/<name>.py. Kept for
# back-compat — if someone drops a ``providers/foo.py`` into an
# editable install, it still works without the plugin layout.
try:
import pkgutil

import providers as _pkg

for _importer, modname, _ispkg in pkgutil.iter_modules(_pkg.__path__):
if modname.startswith("_") or modname == "base":
continue
try:
importlib.import_module(f"providers.{modname}")
except ImportError as exc:
logger.warning(
"Failed to import legacy provider module %s: %s", modname, exc
)
except Exception:
pass

_discovered = True
70 changes: 70 additions & 0 deletions tests/providers/test_plugin_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import importlib
import sys
import threading
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path

import pytest
Expand Down Expand Up @@ -143,3 +145,71 @@ def test_general_plugin_manager_skips_model_provider_kind(tmp_path, monkeypatch)
# No import means the module must NOT be in the plugins list as a loaded one.
# We check that the general loader didn't crash and didn't raise from the
# broken __init__.py.


class TestDiscoverProvidersToctouRace:
"""Regression tests for the TOCTOU race in _discover_providers().

Before the fix, _discovered was set to True before the discovery work
ran, so a second thread could enter _discover_providers(), see
_discovered=False, then race the first thread and observe a partially
populated registry (or observe _discovered=True with an empty registry
because the flag was published before the work finished).
"""

def test_concurrent_calls_consistent(self, monkeypatch):
"""50 threads calling _discover_providers() concurrently must all
observe the same final _discovered state with no torn reads."""
import providers as prov_mod

monkeypatch.setattr(prov_mod, "_discovered", False)
monkeypatch.setattr(prov_mod, "_REGISTRY", {})
monkeypatch.setattr(prov_mod, "_ALIASES", {})

n = 50
barrier = threading.Barrier(n)
results: list[bool] = []
lock = threading.Lock()

def worker():
barrier.wait()
prov_mod._discover_providers()
with lock:
results.append(prov_mod._discovered)

with ThreadPoolExecutor(max_workers=n) as ex:
futures = [ex.submit(worker) for _ in range(n)]
for f in futures:
f.result()

assert all(results), "Every thread must see _discovered=True after discovery"
assert len(results) == n

def test_flag_set_after_registry_populated(self, monkeypatch):
"""_discovered must not become True before the registry work completes.

We interpose on _import_plugin_dir to record whether _discovered was
already True when the first plugin was being imported — if so the
flag was published too early.
"""
import providers as prov_mod

monkeypatch.setattr(prov_mod, "_discovered", False)
monkeypatch.setattr(prov_mod, "_REGISTRY", {})
monkeypatch.setattr(prov_mod, "_ALIASES", {})

flag_during_import: list[bool] = []
original_import = prov_mod._import_plugin_dir

def spying_import(plugin_dir, source):
flag_during_import.append(prov_mod._discovered)
return original_import(plugin_dir, source)

monkeypatch.setattr(prov_mod, "_import_plugin_dir", spying_import)
prov_mod._discover_providers()

# If any plugin directory was found, the flag must have been False
# during its import (flag set last, after all work).
assert all(
not v for v in flag_during_import
), "_discovered was True during plugin import — flag published too early"
Loading