crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] A sync handler registered as functools.partial crashes the event bus instead of logging its error

Open houyunsun opened this issue 1 month ago • 1 comments

Description

CrewAIEventsBus._call_handlers() formats the failure message with handler.__name__. Sync handlers may be registered as functools.partial(...), and a partial has no __name__, so building that message raises AttributeError: 'functools.partial' object has no attribute '__name__'.

The AttributeError is raised inside _call_handlers(), not inside the handler, so nothing catches it: the exception replaces the original handler error and is re-raised into the caller of emit() / replay() (the future the bus hands back carries it instead of None). The original exception is never printed. Nothing about the event system itself changes -- a failing handler is supposed to be logged and swallowed, as is_call_handler_safe() does.

The async path already guards this (getattr(handler, '__name__', handler) in _acall_handlers()), and handler_graph.py does the same when it reports circular dependencies, so the sync path looks like an oversight.

Steps to reproduce

import functools

from crewai.events.event_bus import CrewAIEventsBus
from crewai.events.types.crew_events import CrewKickoffStartedEvent

bus = CrewAIEventsBus()
ev = CrewKickoffStartedEvent(crew_name="c", inputs={})


def boom(source, event):
    raise ValueError("handler exploded")


bus.register_handler(CrewKickoffStartedEvent, functools.partial(boom))
future = bus.emit(object(), ev)
future.result(timeout=5)  # AttributeError, not the ValueError

Partial is a natural way to register a handler when you want to bind some context up front, e.g. partial(my_handler, sink=queue).

Expected behavior

The ValueError is logged through _console.print like any other sync handler error, and emit() returns normally.

Environment

  • crewai main at 97bbfb6
  • Python 3.11, Linux

I'll send a one-line fix with a regression test.

houyunsun avatar Oct 30 '26 12:10 houyunsun

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 29 '26 12:11 github-actions[bot]