[BUG] flush() returns before sync event handlers finish when async handlers are also registered
Description
In CrewAIEventsBus.emit(), when an event has both sync and async handlers registered, the future for the sync handlers (submitted to the thread-pool executor) is neither returned to the caller nor registered via _track_future(). As a result the sync handlers are invisible to flush() and to shutdown(wait=True) (which calls flush() first):
flush()can returnTrue(all done) while a sync handler is still running.- By the time the caller observes completion, sync-handler errors may not have been reported yet.
replay() already tracks the sync future unconditionally on exactly the same code path (self._track_future(sync_future) before the if not async_handlers: check) -- emit() is missing that one line.
Steps to Reproduce
- Register one sync handler (sleeps 0.3 s) and one async handler (returns immediately) for the same event type.
- Call
future = crewai_event_bus.emit(source, event). - Call
crewai_event_bus.flush(timeout=5)and measure how long it takes. - Check whether the sync handler has finished when
flush()returns.
Expected behavior
flush() should wait until every submitted handler future completes -- including sync-handler futures when async handlers are also registered -- so that flush() is True implies all handlers have finished, consistent with sync-only emission and with replay().
Screenshots/Code snippets
Minimal reproduction (bug on main @ 86dca29, file lib/crewai/src/crewai/events/event_bus.py):
import threading, time
from crewai.events.base_events import BaseEvent
from crewai.events.event_bus import crewai_event_bus
class MyEvent(BaseEvent):
type: str = "repro_mixed_handlers"
sync_done = threading.Event()
def slow_sync_handler(source, event):
time.sleep(1.5)
sync_done.set()
async def fast_async_handler(source, event):
pass
crewai_event_bus.register_handler(MyEvent, slow_sync_handler)
crewai_event_bus.register_handler(MyEvent, fast_async_handler)
crewai_event_bus.emit(object(), MyEvent())
ok = crewai_event_bus.flush(timeout=10) # returns True immediately
print("flush:", ok, "sync handler finished:", sync_done.is_set())
sync_done.wait(10) # finishes ~1.5 s later
Actual output:
flush: True sync handler finished: False
Operating System
Windows 10
Python Version
3.12
crewAI Version
1.15.34 (installed from source, main @ 86dca29)
crewAI Tools Version
1.15.34
Virtual Environment
Venv
Evidence
flush()returnedTruein ~0.00 s while the sync handler was still insidetime.sleep(); the handler only completed ~1.5 s later (see actual output above).- Root cause in
emit()(lib/crewai/src/crewai/events/event_bus.py, currentmain):
sync_future = self._sync_executor.submit(
ctx.run, self._call_handlers, source, event, sync_handlers, state
)
if not async_handlers:
return self._track_future(sync_future) # sync future only tracked in the sync-only case
- Contrast with
replay()on the same path, which tracks unconditionally:
sync_future = self._sync_executor.submit(
ctx.run, self._call_handlers, source, event, sync_handlers, state
)
self._track_future(sync_future)
if not async_handlers:
return sync_future
Possible Solution
Track the sync future unconditionally in emit(), matching replay():
self._track_future(sync_future)
if not async_handlers:
return sync_future
This is a minimal one-line reordering; the sync-only return value is unchanged (the same tracked future object). I have a fix with regression tests ready and will submit a PR referencing this issue.
Additional context
Found while auditing emit()/replay() symmetry in the events module. The same gap affects shutdown(wait=True) because it calls flush() first: the executor can be shut down while a mixed sync+async emission still has a sync handler in flight.
Verified against main @ 86dca29 and reproduced with a regression test — the analysis matches the issue exactly:
emit()(lib/crewai/src/crewai/events/event_bus.py:632-637) submits the sync-handler future but only calls_track_future()in thenot async_handlersbranch, so on mixed sync+async events the sync future is invisible toflush()andshutdown(wait=True).replay()tracks the same future unconditionally (:716-719) — the asymmetry is within one file.
Fix is the one-liner the issue points at (track before the sync-only early return), plus a regression test that registers a sleeping sync handler alongside an immediate async handler and asserts flush() doesn't return True before the sync handler completes. Verified red on main, green with the fix; full tests/events/ suite (194 tests) passes.
Branch fix/track-sync-future-mixed-emit is ready — opening the PR now.
(This investigation and the patch were produced with AI assistance — please apply the llm-generated label.)