mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-01 13:18:10 +00:00
Two related races in flow.usage_metrics aggregation: 1. Paused kickoff dropped its listener. When kickoff_async returned a HumanFeedbackPending, the finally detached the listener even though the bus dispatches LLM event handlers on a thread pool that emit does not wait on. Any pre-pause LLM call whose handler future was still queued would silently lose its tokens. Fix: track a paused_for_feedback flag in kickoff_async and skip the detach when set. The listener stays attached on the instance so late events continue to accumulate. 2. Stale handlers from a prior kickoff could bleed into a later one. The handler closure captured flow_ref and wrote into flow._aggregated_usage_metrics. If a handler from kickoff #1 was still queued when kickoff #2 reset the accumulator, it would contaminate the new run's totals. Fix: snapshot a per-flow _usage_epoch in the handler closure at attach time. kickoff_async bumps the epoch before attaching, so any in-flight handler from a prior kickoff sees its stale snapshot and bails out.