[BUG] Filtered @before_*/@after_* hook decorators cannot be unregistered via unregister_*_hook
Los mantenedores suelen responder en 1 día
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 30/100
Línea de trabajo
Start in lib/crewai/src/crewai/hooks/decorators.py at _create_hook_decorator, where the filtered_hook wrapper is registered but never linked back to the returned function. Compare with on and unregister in lib/crewai/src/crewai/hooks/dispatch.py, then check the four unregister_*_hook helpers in tool_hooks.py and llm_hooks.py. Done means the unregister calls return True and the registry is empty after the reproduction snippet. A linked pull request (#9683) is already open, so check it before starting.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Description
When registering a global hook using any of the filtered hook decorators (@before_tool_call(tools=...), @after_tool_call(tools=...), @before_llm_call(agents=...), or @after_llm_call(agents=...)), _create_hook_decorator (lib/crewai/src/crewai/hooks/decorators.py:54-75) registers the internal filtered_hook wrapper while returning the original function f.
Unlike @on(...) in lib/crewai/src/crewai/hooks/dispatch.py:406 (which stores func._registered_hook = hook and resolves it in dispatch.unregister), _create_hook_decorator does not attach _registered_hook, and all four unregister_*_hook helpers in tool_hooks.py and llm_hooks.py call .remove(hook) directly. As a result, calling unregister_before_tool_call_hook(fn) (or the other three unregister functions) always returns False and leaks the hook in the global registry.
Steps to Reproduce
- Decorate a top-level function with
@before_tool_call(tools=["delete_file"])(or@after_tool_call,@before_llm_call(agents=...),@after_llm_call(agents=...)). - Call
unregister_before_tool_call_hook(my_hook)(or the matchingunregister_*_hookhelper). - Observe that it returns
Falseandget_before_tool_call_hooks()still contains the wrapper hook.
Expected behavior
Calling unregister_before_tool_call_hook, unregister_after_tool_call_hook, unregister_before_llm_call_hook, or unregister_after_llm_call_hook with a function decorated by the corresponding filtered @before_*/@after_* decorator should remove the registered wrapper and return True, matching dispatch.unregister behavior for @on(...).
Screenshots/Code snippets
from crewai.hooks.decorators import before_tool_call
from crewai.hooks.tool_hooks import (
clear_all_tool_call_hooks,
get_before_tool_call_hooks,
unregister_before_tool_call_hook,
)
clear_all_tool_call_hooks()
@before_tool_call(tools=["delete_file"])
def guard_delete(context):
return False
removed = unregister_before_tool_call_hook(guard_delete)
print("removed:", removed, "remaining:", len(get_before_tool_call_hooks()))
Actual: removed: False remaining: 1
Expected: removed: True remaining: 0
Operating System
macOS Sonoma
Python Version
3.12
crewAI Version
1.15.44 (main 6ecb87390)
crewAI Tools Version
N/A
Virtual Environment
Venv
Evidence
lib/crewai/src/crewai/hooks/decorators.py:54-75: registersfiltered_hookand returnsfwithout storingf._registered_hook = filtered_hook.lib/crewai/src/crewai/hooks/tool_hooks.py:298-347andlib/crewai/src/crewai/hooks/llm_hooks.py:313-362:unregister_*_hookcalls.remove(hook)directly instead of delegating tocrewai.hooks.dispatch.unregister.- Running the snippet above prints
removed: False remaining: 1.
Possible Solution
Store f._registered_hook = filtered_hook in _create_hook_decorator when registering a filtered global hook (mirroring dispatch.on), and delegate unregister_before_tool_call_hook, unregister_after_tool_call_hook, unregister_before_llm_call_hook, and unregister_after_llm_call_hook to crewai.hooks.dispatch.unregister.
Additional context
I have a minimal fix with regression tests ready and will open a linked PR. AI assistance was used in preparing this report (llm-generated).
- Lenguaje dominante
- Python
- Estrellas
- 61.2k
- Forks
- 9.2k
- Merge medio
- 1 d 14 h
- PR fusionados (30 d)
- 180
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de crewAIInc/crewAI
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
[FEATURE] Add Straitly as a native OpenAI-compatible LLM providerPosiblemente ocupada @HashimSyedUBC la tomó hace 2 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
[BUG] Native OpenAI sync streaming with response_model returns an empty string and emits no completed event when the parse is missingPosiblemente ocupada @basil-k-aji-dev la tomó hace 2 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
[BUG] Native Azure provider ignores the seed settingPosiblemente ocupada @basil-k-aji-dev la tomó hace 2 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
crewAIInc/crewAI#9787 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
[BUG] LiteLLM path reports 0 cached and reasoning tokens in the usage summaryPosiblemente ocupada @basil-k-aji-dev la tomó hace 2 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
Todos los issues de crewAIInc/crewAI
Issues similares
-
bug harness
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
mruangutai/harness#2687 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
modal-labs/modal-client#4158 ·
-
good first issue
Dificultad 1/5 Menos de una hora Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 Medio día Aptitud para principiantes 72/100
The-Swarm-Corporation/swarms-rs#168 ·
Los mantenedores suelen responder en 1 día
-
[BUG] SMTP listener now binds to EMAIL_PORT instead of 2525 since the gunicorn switch 5.0.3-ls373Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
linuxserver/docker-healthchecks#137 · 1 comentario ·