Repository navigation
fix(hooks): reject async hooks before intercepted calls - #10258
ADITYA (adity982) wants to merge 1 commit into
Conversation
Nick (surlymochan)
left a comment
There was a problem hiding this comment.
Verified against main (lib/crewai/src/crewai/hooks/dispatch.py L33, L276-283; tool_hooks.py L173-190): the old path passed whatever the hook returned straight to the reducer, and the default reducer treats any non-None result as a payload modification — so an async hook's un-awaited coroutine was stored as ctx.payload and the tool call proceeded (fail-open, plus a never-awaited coroutine). The fix detects awaitables, closes them when possible, and raises HookAborted, which run_before_tool_call_hooks converts to True (blocked) per its documented contract. inspect is already imported at L33, so no new import was needed.
Non-blocking: close() only applies to awaitables that expose it (plain coroutines) — an already-created Task/Future has no close, so it keeps running in the background after the abort and its side effects still land. Worth a comment if hooks are ever handed pre-spawned tasks; the common async def case is covered.
Summary
Fixes #10247.
Hooks are documented as synchronous, but an async pre-tool hook currently returns a coroutine that is truthy, allowing the tool call to proceed even when the hook intends to deny it. Detect awaitable hook results, close closeable coroutines to avoid unawaited-coroutine warnings, and abort the intercepted operation with an actionable reason.
Validation