Skip to content

fix(hooks): reject async hooks before intercepted calls - #10258

Draft
ADITYA (adity982) wants to merge 1 commit into
crewAIInc:mainfrom
adity982:fix/reject-async-hooks
Draft

ADITYA (adity982) wants to merge 1 commit into
crewAIInc:mainfrom
adity982:fix/reject-async-hooks

Conversation

@adity982

Copy link
Copy Markdown

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

  • Focused hook regression plus existing deny behavior: 2 passed.
  • Ruff check: passed.
  • Ruff format check and git diff check: passed.
  • Full hook test file: 25 passed, 3 failed in unrelated native-tool integration tests because the Windows pytest network guard blocks asyncio socket-pair setup.
  • Dispatcher tests: 15 passed, 2 telemetry tests failed because event-loop socket-pair setup is blocked by the same Windows network guard.

@surlymochan Nick (surlymochan) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] async hook functions are never awaited, so an async before_tool_call deny hook lets the tool call run

2 participants