Skip to content

The agent that grows with you

refactor(config): drop dead guards in the lock-free config fast paths

kshitijk4poor09fe2c1326d4
diff --git a/hermes_cli/config.py b/hermes_cli/config.py
index babd04f4213..d676a07080b 100644
--- a/hermes_cli/config.py
+++ b/hermes_cli/config.py
@@ -2299,22 +2299,10 @@ def _merge_managed_overlay(expanded: Dict[str, Any]) -> Tuple[Dict[str, Any], An
def _load_config_impl(*, want_deepcopy: bool) -> Dict[str, Any]:
- # Lock-free fast path for cache hits.
- #
- # A cache hit costs ~0.024ms, but it used to sit behind `_CONFIG_LOCK` —
- # which `save_config()` holds across an atomic YAML write. Measured: the
- # same cached read takes 10010ms when another thread holds the lock. On a
- # gateway this lands on the event loop, because the per-message hook path
- # (`invoke_hook` -> `_resolve_hook_callback_timeout`) reads config, so one
- # background config write stalls every inbound message for the duration.
- #
- # The lock never protected the cache dict itself: CPython dict get/setitem
- # are atomic under the GIL, and the cached tuple is replaced wholesale
- # rather than mutated in place, so a reader sees either the complete old
- # tuple or the complete new one. The lock's real job is serializing the
- # rebuild (parse + merge + expand) and the writers. Worst case on a race
- # is a redundant rebuild, which the locked path below re-checks and
- # collapses.
+ # Lock-free fast path for cache hits — same publication contract as `_read_raw_config_impl`
+ # above (whole-tuple replace, `_CONFIG_LOCK` only serializes rebuilds and writers). A hit costs
+ # ~0.024ms; behind a lock held by `save_config()` the same read measured 10010ms, and on a
+ # gateway that stalls every inbound message's hook path. A lost race falls through to the lock.
try:
config_path = get_config_path()
path_key = str(config_path)
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.py
index bece644de2f..e998382ee15 100644
--- a/hermes_cli/plugins.py
+++ b/hermes_cli/plugins.py
@@ -1134,11 +1134,11 @@ del _name, _method
# Keyed by the scope-resolved config path (like `_LOAD_CONFIG_CACHE`) so multiplexed profiles do not
# evict each other. Each value is ONE `(sig, value)` tuple stored with a single dict item assignment
# (atomic under the GIL), so a lock-free reader can never observe a new sig paired with an old value.
-_HOOK_TIMEOUT_CACHE: Dict[str, Tuple[Any, Optional[float]]] = {}
+_HOOK_TIMEOUT_CACHE: Dict[str, Tuple[Any, float]] = {}
def _reset_hook_callback_timeout_cache() -> None:
- """Drop the memoized hook-callback timeouts. For tests and config reloads."""
+ """Drop the memoized hook-callback timeouts (test-only; production relies on the sig check)."""
_HOOK_TIMEOUT_CACHE.clear()
@@ -1161,7 +1161,7 @@ def _resolve_hook_callback_timeout() -> float:
path_key, sig = "", None
cached = _HOOK_TIMEOUT_CACHE.get(path_key) # one read of the published tuple
- if cached is not None and sig is not None and cached[0] == sig and isinstance(cached[1], float):
+ if cached is not None and sig is not None and cached[0] == sig:
return cached[1]
resolved = _resolve_hook_callback_timeout_uncached()