Repository navigation
fix(hooks): fail closed on async hooks instead of letting guarded calls through - #10254
YuShaw (starflowerH) wants to merge 1 commit into
Conversation
…ls through Hooks are documented as synchronous, but _invoke_hook handed the un-awaited coroutine from an async hook to the reducer, so an async before_tool_call deny hook failed open (a coroutine is never False) and an async payload hook stored the coroutine as the payload. Check inspect.isawaitable(result) before the reducer runs; close the coroutine to avoid the 'never awaited' warning and raise HookAborted so the guarded call is denied instead of running. HookAborted is the one exception _invoke_hook re-raises, and run_before_tool_call_hooks maps it to blocked=True. Add DB-free regression tests covering the async deny block, the async payload abort, the absence of the warning, and unchanged sync behavior.
Strix Security Review✅ No open security findings. PR summary: Changes the hook dispatcher in `lib/crewai/src/crewai/hooks/dispatch.py` so a hook that returns an awaitable is treated as an error: the coroutine is closed and `HookAborted` is raised, which makes the guarded tool or model call fail closed instead of silently running. Adds regression tests for async deny hooks, async payload hooks and the absence of "never awaited" warnings. Review summaryThe change adds an awaitable check in Ruled out (3)
Review 1 on this PR · Reviewed by Strix |
Fixes #10247
Problem
Hooks are documented as synchronous, but nothing enforces it.
_invoke_hookcalls
result = hook(ctx)and hands the result to the reducer, so for anasync defhook the reducer receives an un-awaited coroutine:before_tool_call_reduceronly blocks whenresult is False. A coroutineis never
False, so an async hook written to deny a call fails open:the tool or LLM call runs.
_default_reducerreplacesctx.payloadwith any non-Noneresult, so anasync payload hook stores the coroutine object as the payload.
coroutine '...' was never awaited, but nothing tells the userthe hook had no effect. An async guard is an easy mistake to make (the agent
runs async elsewhere, and the hook may call an async API), and it silently
disables the guard.
Solution
In
_invoke_hook— the one path every registration style goes through(
register_*,@on,@before_tool_call, scoped hooks) — checkinspect.isawaitable(result)before the reducer runs. If it is awaitable,close the coroutine (so there is no "never awaited" warning) and raise
HookAbortedwith a clear message naming the hook.HookAbortedis the oneexception
_invoke_hookre-raises rather than swallowing, it propagatesthrough
run_hooks(telemetry outcome "aborted"), andrun_before_tool_call_hooksmaps it toTrue(blocked) — so the guardedcall is denied instead of running.
Why fail closed at run time (option A in the issue) rather than rejecting at
registration (option B): the generic
except Exceptionin_invoke_hookswallows errors, so a
TypeErrorraised there would still fail open; and aruntime awaitable check covers more than a registration-time
iscoroutinefunctioncheck (callable objects with async__call__,functools.partialwrapping an async fn, wrappers that hide the coroutinenature). It also matches the repo's fail-closed tool-hook design. Happy to
rework to option B if maintainers prefer it.
Verification
blocked = True,no warnings); async deny hook does not block (
blocked = False, warningcoroutine 'async_deny' was never awaited). After the fix the async denyhook blocks (
blocked = True) and no warning is emitted.run_before_tool_call_hooks; async payload hook at PRE_MODEL_CALL raisesHookAbortedand leavesctx.payloadunchanged; no "never awaited"warning under
warnings.simplefilter("error"). Each new test fails withoutthe fix (verified via
git stashof the source change only) and passeswith it.
pytest lib/crewai/tests/hooks/test_dispatch.py lib/crewai/tests/hooks/test_tool_hooks.py lib/crewai/tests/hooks/test_hook_abort_propagation.py: 70 passed, 4 failed. The 4 failures are all live-OpenAI integration tests (TestNativeToolCallingHooksIntegration× 3,test_lite_agent_hooks_integration_with_real_tool× 1) that fail with "Failed to connect to OpenAI API: Connection error" in this sandbox; the same 4 fail on the unmodified baseline, so they are not regressions.ruff checkandruff format --checkpass on the changed source file (test files are excluded from ruff by repo config).