[BUG] flush() doesn't wait for sync handlers when sync and async handlers are both registered
Description
When an event has both sync and async handlers registered, CrewAIEventsBus.emit() submits the sync handlers to the thread-pool executor but never calls _track_future() on that sync future. Tracking only happens on the sync-only early-return path. In the mixed path, emit() falls through and returns the asyncio future for the async handlers, so the sync future is orphaned:
flush()only waits on_pending_futures, so it returnsTrueimmediately even though the sync handler is still running.- An exception raised by the sync handler is never surfaced by
flush()— it is silently swallowed in the orphaned future.
replay() tracks the sync future unconditionally, which shows the intended contract; emit() deviates from it.
Steps to Reproduce
- Register one slow sync handler and one fast async handler for the same event type.
- Call
crewai_event_bus.emit(source, event). - Call
crewai_event_bus.flush()and observe the results.
import time
from pydantic import BaseModel
from crewai.events import crewai_event_bus
class Done(BaseModel):
pass
results = []
@crewai_event_bus.on(Done)
def slow_sync(source, event):
time.sleep(1.0)
results.append("sync")
@crewai_event_bus.on(Done)
async def fast_async(source, event):
results.append("async")
crewai_event_bus.emit(object(), Done())
print(crewai_event_bus.flush()) # True — returned immediately
print(results) # ['async'] — sync handler still sleeping
Expected behavior
flush() should wait for the sync handlers to complete (returning True only after all handlers finished) and should report exceptions raised by sync handlers, same as it does for tracked async futures and as replay() does.
Screenshots/Code snippets
emit() on main — the sync future is only tracked on the sync-only path:
if sync_handlers:
if event_type is LLMStreamChunkEvent:
self._call_handlers(source, event, sync_handlers, state)
else:
ctx = contextvars.copy_context()
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) # mixed path never reaches this
if async_handlers:
return self._track_future(
asyncio.run_coroutine_threadsafe(...) # sync_future is orphaned
)
flush() waits only on _pending_futures:
with self._futures_lock:
futures_to_wait = list(self._pending_futures)
...
done, not_done = wait_futures(futures_to_wait, timeout=timeout)
Operating System
Windows 11 (bug is platform-independent, verified by code-path analysis)
Python Version
3.12
crewAI Version
main @ 5048e25 (source install)
crewAI Tools Version
N/A (not used in repro)
Virtual Environment
Venv
Evidence
Observed: flush() returned True immediately while the sync handler was still running ('sync' missing from results); a sync handler raising an exception is never reported. After tracking the sync future, flush() waits for it and surfaces its exceptions. Regression tests are included in PR #8712 (test_flush_waits_for_pending_sync_handler, test_flush_reports_sync_handler_exception).
Possible Solution
Track the sync future unconditionally in emit(), mirroring replay():
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 self._track_future(sync_future)
+ return sync_future
The sync-only early-return path is behaviorally unchanged (same future object returned); the mixed path now lets flush() wait on and surface sync-handler outcomes.
Additional context
Fix is already implemented and tested in PR #8712 (2 files, +78/-1). Strix security review on that PR found no open findings.
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.