Repository navigation
fix(events): track the sync handler future when async handlers are registered - #8961
hdu_zhou (zuizhuai) wants to merge 5 commits into
Conversation
Strix Security ReviewWarning This pull request has 103 commits after the last Strix review ( ✅ No open security findings. PR summary: Fixes event-bus flush semantics so `emit()` registers the sync-handler future even when async handlers are also present, letting `flush()`/`shutdown(wait=True)` wait for sync handlers; adds two tests covering mixed and sync-only emissions. Review summaryThe PR makes Ruled out (3)
Review 1 on this PR · Reviewed by Strix |
|
Vidit Ostwal (@Vidit-Ostwal) is this still wanted? There is no other open PR against #8949 ( |
Fixes #8949.
Problem
CrewAIEventsBus.emit()submits the sync handlers to the thread-pool executor, but it only returns that future to_track_future()in the sync-only branch:When an event has both sync and async handlers, the sync future is returned to nobody and never registered as pending, so
flush()— andshutdown(wait=True), which callsflush()first — can reportTruewhile a sync handler is still running, and sync-handler errors can surface after the caller believes the emission finished.replay()already tracks the same future unconditionally before itsif not async_handlerscheck, so the two dispatch paths disagreed on identical code.Change
emit()now callsself._track_future(sync_future)unconditionally and returns that future when there are no async handlers, mirroringreplay(). The value returned for a sync-only emission is the same future as before; mixed emissions still return the async future.Intentionally not done:
replay(),aemit(), or the dependency-aware path;Tests
New file
lib/crewai/tests/events/test_event_bus_flush.py:test_flush_waits_for_sync_handler_when_async_handler_is_also_registered: registers a sync handler that sleeps 0.2 s and sets athreading.Event, plus a no-op async handler, emits, then assertsflush(timeout=5)returnsTrueand the event is set. Onmainthis fails:flush()returns immediately while the handler is still insidetime.sleep, so the event is unset.test_flush_waits_for_sync_handler_without_async_handlers: control for the sync-only path, green before and after.Local results (Windows 11, Python 3.13.1):
The 2 failures (
test_pop_empty_stack_returns_none,test_depth_limit_exceeded_raises) and 2 errors (two Flow checkpoint-resume tests) reproduce identically on unmodifiedmainwith the same invocation, so they are pre-existing in this environment and unrelated to the change.Lint:
ruff checkon the changed files →All checks passed!;ruff format --check→ already formatted. Note the repository ruff config excludeslib/crewai/tests/, so the new test file is covered by neither.Environment limitation: only
lib/crewaiwas checked out here (sparse clone) and the package could not be installed editable (hatchling metadata step fails in this sandbox), so the suite was run withPYTHONPATH=lib/crewai/srcand a hand-installed dependency subset. The LLM/VCR-based suites intests/events/(e.g.test_event_ordering.py) were therefore not executed — CI will be the first full run.Note
Medium Risk
Changes core event dispatch completion semantics for mixed sync/async handlers; low code surface but affects shutdown and flush guarantees across the event bus.
Overview
Fixes a race where
CrewAIEventsBus.emit()could finishflush()(and shutdown) while a sync handler was still running when the same event also had async handlers.Previously the thread-pool future for sync handlers was only added to
_pending_futuresin the sync-only branch; mixed emissions returned only the async future, soflush()could succeed before slow sync work completed.emit()now always calls_track_future(sync_future)after submitting sync handlers (matchingreplay()), while still returning the async future when both handler types exist.Adds
test_event_bus_flush.pywith a mixed-handler regression test and a sync-only control.Reviewed by Cursor Bugbot for commit ca10f52. Bugbot is set up for automated code reviews on this repo. Configure here.