Skip to content

The agent that grows with you

fix(plugins): keep one worker per callback while a timed-out worker is still running

kshitijk4poorcdd58810a406
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.py
index 000102babed..699ef70f5e6 100644
--- a/hermes_cli/plugins.py
+++ b/hermes_cli/plugins.py
@@ -1165,6 +1165,7 @@ class PluginManager(PluginLoaderMixin, PluginDispatchMixin, PluginLedgerMixin):
# In-flight / recently-timed-out hook callbacks keyed by (hook_name, id(cb)) so a stuck
# policy hook cannot spawn a new abandoned thread on every fire.
self._hook_running_callbacks: Dict[tuple, object] = {}
+ self._hook_abandoned: Dict[tuple, set] = {}
self._hook_timeout_suppressed_until: Dict[tuple, float] = {}
self._hook_timeout_lock = threading.Lock()
self._hook_timeout_suppression_seconds = _HOOK_TIMEOUT_SUPPRESSION_SECONDS
diff --git a/hermes_cli/plugins_dispatch.py b/hermes_cli/plugins_dispatch.py
index 53d14777226..996e1b77b44 100644
--- a/hermes_cli/plugins_dispatch.py
+++ b/hermes_cli/plugins_dispatch.py
@@ -140,7 +140,7 @@ _MAX_HOOK_CALLBACK_TIMEOUT_SECS = 600.0
_HOOK_SKIPPED = object() # returned by _run_hook_callback_bounded on skip/timeout
-def _hook_call_identity(kwargs: Dict[str, Any]) -> Any:
+def _hook_call_identity(kwargs: Dict[str, Any]) -> Optional[str]:
"""Identity of the call this callback fires for, or ``None`` when the event has none.
Concurrent invocations of the same tool in one session must not collapse into one
@@ -234,7 +234,10 @@ class PluginDispatchMixin:
token = object()
with self._hook_timeout_lock:
suppressed_until = self._hook_timeout_suppressed_until.get(suppression_key)
- running = gate_key in self._hook_running_callbacks
+ # A worker abandoned on timeout still holds a thread; a fresh call id must not
+ # start a second one for the same callback, or a hung plugin leaks a thread per call.
+ running = (gate_key in self._hook_running_callbacks
+ or bool(self._hook_abandoned.get(suppression_key)))
if (suppressed_until is not None and suppressed_until > time.monotonic()) or running:
logger.warning(
"Hook '%s' callback %s skipped after previous "
@@ -253,6 +256,11 @@ class PluginDispatchMixin:
with self._hook_timeout_lock:
if self._hook_running_callbacks.get(gate_key) is token:
self._hook_running_callbacks.pop(gate_key, None)
+ abandoned = self._hook_abandoned.get(suppression_key)
+ if abandoned is not None:
+ abandoned.discard(gate_key)
+ if not abandoned:
+ self._hook_abandoned.pop(suppression_key, None)
def _runner() -> None:
try:
@@ -277,6 +285,7 @@ class PluginDispatchMixin:
# See #6622.
self._hook_timeout_suppressed_until[suppression_key] = (
time.monotonic() + self._hook_timeout_suppression_seconds)
+ self._hook_abandoned.setdefault(suppression_key, set()).add(gate_key)
logger.warning(
"Hook '%s' callback %s timed out after %gs — skipping", hook_name, callback_name, timeout)
return _HOOK_SKIPPED
diff --git a/hermes_cli/plugins_ledger.py b/hermes_cli/plugins_ledger.py
index d9ffbfee693..f259695b79d 100644
--- a/hermes_cli/plugins_ledger.py
+++ b/hermes_cli/plugins_ledger.py
@@ -297,5 +297,6 @@ class PluginLedgerMixin:
self._context_engine = None
with self._hook_timeout_lock:
self._hook_running_callbacks.clear()
+ self._hook_abandoned.clear()
self._hook_timeout_suppressed_until.clear()
self._discovered = False