Skip to content

The agent that grows with you

fix(plugins): a hung pre_tool_call guard no longer blocks every tool call until restart

teknium137555209167d
diff --git a/hermes_cli/plugins_dispatch.py b/hermes_cli/plugins_dispatch.py
index e49b3cef263..837dcb3fc55 100644
--- a/hermes_cli/plugins_dispatch.py
+++ b/hermes_cli/plugins_dispatch.py
@@ -50,6 +50,8 @@ _HOOK_TIMEOUT_FAIL_CLOSED_HOOKS: Set[str] = {"pre_tool_call"}
_HOOK_CALLER_THREAD_HOOKS: Set[str] = {"subagent_stop"}
# After a timeout, suppress the same callback this long so a hung hook cannot pile up threads.
_HOOK_TIMEOUT_SUPPRESSION_SECONDS = 60.0
+# Live workers a hung callback may accumulate before it is skipped outright (#105223 / #98382).
+_HOOK_MAX_ABANDONED_WORKERS = 3
_PRE_TOOL_CALL_TIMEOUT_BLOCK_MESSAGE = "pre_tool_call plugin callback timed out or is still running"
@@ -261,8 +263,9 @@ class PluginDispatchMixin:
self, hook_name: str, cb: Callable, kwargs: Dict[str, Any], timeout: float
) -> Any:
"""Run one callback on a daemon worker with a wall-clock cap; ``_HOOK_SKIPPED`` when
- suppressed, still running, timed out (worker abandoned, never joined), or the worker
- could not be started. Exceptions propagate."""
+ suppressed, still running for this call id, over the abandoned-worker cap, timed out
+ (worker abandoned, never joined), or the worker could not be started. Exceptions
+ propagate."""
callback_name = getattr(cb, "__name__", repr(cb))
# Suppression is a fact about the CALLBACK — a hung one must keep its back-off —
# so that key stays coarse. The gate must instead tell CONCURRENT CALLS apart.
@@ -271,15 +274,25 @@ class PluginDispatchMixin:
token = object()
with self._hook_timeout_lock:
suppressed_until = self._hook_timeout_suppressed_until.get(suppression_key)
- # 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:
+ if (gate_key in self._hook_running_callbacks
+ or (suppressed_until is not None and suppressed_until > time.monotonic())):
logger.warning(
"Hook '%s' callback %s skipped after previous "
"timeout or while still running", hook_name, callback_name)
return _HOOK_SKIPPED
+ # Workers abandoned on timeout still hold threads. Once the suppression window has
+ # passed, a fresh call id may start a new worker (a hung guard must not fail every
+ # later tool call closed until restart, #105223), but only up to a small cap per
+ # callback — expiring the bookkeeping while the hung worker lives must not leak a
+ # thread per call (#98382). At the cap the callback keeps being skipped (fail-closed
+ # for pre_tool_call) until one of its workers finishes and releases its slot.
+ abandoned = self._hook_abandoned.get(suppression_key)
+ if abandoned and len(abandoned) >= _HOOK_MAX_ABANDONED_WORKERS:
+ logger.warning(
+ "Hook '%s' callback %s (%s) skipped: %d abandoned worker(s) still running — "
+ "the plugin is hung; fix or disable it (retried when a worker finishes)",
+ hook_name, callback_name, getattr(cb, "__module__", "unknown plugin"), len(abandoned))
+ return _HOOK_SKIPPED
if suppressed_until is not None:
self._hook_timeout_suppressed_until.pop(suppression_key, None)
self._hook_running_callbacks[gate_key] = token
diff --git a/tests/hermes_cli/test_plugins.py b/tests/hermes_cli/test_plugins.py
index 3455b75ae06..5440f40a148 100644
--- a/tests/hermes_cli/test_plugins.py
+++ b/tests/hermes_cli/test_plugins.py
@@ -1469,9 +1469,14 @@ class TestForceReloadSymmetry:
hold.set()
first.join(5.0)
- def test_hung_worker_blocks_new_call_identity_after_suppression(self, monkeypatch):
- """A worker abandoned on timeout still occupies its callback: a later call with a
- fresh id must be skipped, not given a second thread (one leak, not one per call)."""
+ def test_hung_worker_caps_new_call_identities_after_suppression(self, monkeypatch, caplog):
+ """Workers abandoned on timeout still occupy their callback: once the suppression window
+ has passed, later calls with fresh ids may start a replacement, but only up to
+ ``_HOOK_MAX_ABANDONED_WORKERS`` live ones — a hung plugin leaks a bounded few threads,
+ never one per call (#98382), and past the cap it is skipped with a warning that names
+ the callback (#105223)."""
+ import hermes_cli.plugins_dispatch as dispatch
+
monkeypatch.setattr(
"hermes_cli.plugins._resolve_hook_callback_timeout", lambda: 0.1
)
@@ -1488,10 +1493,44 @@ class TestForceReloadSymmetry:
mgr._hook_timeout_suppression_seconds = 0.0 # isolate the gate from suppression
mgr._hooks["post_tool_call"] = [blocker]
- assert mgr.invoke_hook("post_tool_call", tool_name="read_file", tool_call_id="call-a") == []
- assert mgr.invoke_hook("post_tool_call", tool_name="read_file", tool_call_id="call-b") == []
+ with caplog.at_level(logging.WARNING, logger="hermes_cli.plugins"):
+ for i in range(dispatch._HOOK_MAX_ABANDONED_WORKERS + 3):
+ assert mgr.invoke_hook("post_tool_call", tool_name="read_file", tool_call_id=f"call-{i}") == []
- assert len(starts) == 1
+ assert len(starts) == dispatch._HOOK_MAX_ABANDONED_WORKERS
+ assert "blocker" in caplog.text and "abandoned worker(s) still running" in caplog.text
+ hold.set()
+
+ def test_hung_worker_does_not_fail_closed_forever(self, monkeypatch):
+ """One never-returning pre_tool_call guard must not block every later tool call until
+ restart: after the suppression window a fresh call id runs a new worker, so a callback
+ that has recovered decides again (#105223)."""
+ import time
+
+ from hermes_cli.plugins import _PRE_TOOL_CALL_TIMEOUT_BLOCK_MESSAGE
+
+ monkeypatch.setattr(
+ "hermes_cli.plugins._resolve_hook_callback_timeout", lambda: 0.1
+ )
+ hold = threading.Event()
+ starts = []
+
+ def guard(**_kwargs):
+ starts.append(1)
+ if len(starts) == 1:
+ hold.wait(timeout=10.0) # the first fire hangs for good
+ return None # later fires decide: allow
+
+ mgr = PluginManager()
+ mgr._hook_timeout_suppression_seconds = 0.2
+ mgr._hooks["pre_tool_call"] = [guard]
+
+ blocked = [{"action": "block", "message": _PRE_TOOL_CALL_TIMEOUT_BLOCK_MESSAGE}]
+ assert mgr.invoke_hook("pre_tool_call", tool_name="read_file", tool_call_id="call-a") == blocked
+ assert mgr.invoke_hook("pre_tool_call", tool_name="read_file", tool_call_id="call-b") == blocked # in window
+ time.sleep(0.3) # suppression window passes; the first worker is still hung
+ assert mgr.invoke_hook("pre_tool_call", tool_name="read_file", tool_call_id="call-c") == []
+ assert len(starts) == 2
hold.set()
def test_worker_finishing_at_timeout_does_not_leave_phantom_abandoned_entry(self, monkeypatch):
diff --git a/website/docs/user-guide/features/hooks.md b/website/docs/user-guide/features/hooks.md
index a3d235f3321..d29dce466b9 100644
--- a/website/docs/user-guide/features/hooks.md
+++ b/website/docs/user-guide/features/hooks.md
@@ -572,7 +572,7 @@ Shell hooks also accept the Claude Code-compatible format:
Both formats are normalized internally to `{"action": "modify", "args": {...}}`.
-If a `pre_tool_call` callback exceeds `plugins.hook_callback_timeout` (or is still running from a previous timed-out fire), Hermes **fails closed**: the tool is blocked with a timeout message rather than proceeding without a policy decision.
+If a `pre_tool_call` callback exceeds `plugins.hook_callback_timeout` (or is still running from a previous timed-out fire), Hermes **fails closed**: the tool is blocked with a timeout message rather than proceeding without a policy decision. The same applies to a callback that raises: the block message names the callback and the error. A hung callback is skipped for a 60s suppression window; after that a new tool call runs it again (up to three abandoned workers per callback, so a permanently hung plugin blocks tool calls with a warning naming it instead of silently wedging the agent until restart).
**Use cases:** Logging, audit trails, tool call counters, blocking dangerous operations, rate limiting, per-user policy enforcement, argument sanitization, path rewriting, injecting default parameters.