Repository navigation
Replace deprecated asyncio.get_event_loop() with asyncio.get_running_loop() in coroutine contexts - #8910
Replace deprecated asyncio.get_event_loop() with asyncio.get_running_loop() in coroutine contexts#8910LVsarff wants to merge 1 commit into
Conversation
…loop() in coroutine contexts
Strix Security Review✅ No open security findings. PR summary: Replaces three deprecated `asyncio.get_event_loop()` calls with `asyncio.get_running_loop()` in the structured tool invocation, ChromaDB async lock, and async streaming state setup, to stay compatible with Python 3.12+ deprecation and 3.14 behavior. Review summaryReviewed all three changed lines. Each replacement is behavior-identical: every changed site executes inside a running event loop ( Review 1 on this PR · Reviewed by Strix |
|
Thanks for the pull request. First-time contributors need an associated open issue before we can review a PR.
See the contributing guide. |
Summary
asyncio.get_event_loop()is deprecated since Python 3.12 (and raisesRuntimeErrorin Python 3.14 when no current loop exists). In coroutine contexts the recommended replacement isasyncio.get_running_loop(), which returns exactly the same loop with no deprecation.This PR replaces the three usages in
lib/crewaithat are always executed inside a running event loop:src/crewai/tools/structured_tool.py— insideasync def _arun, so a loop is always runningsrc/crewai/rag/chromadb/client.py— insideasync def _alocked, so a loop is always runningsrc/crewai/utilities/streaming.py— theuse_async=Truebranch ofcreate_streaming_state, which is only reached from async kickoff paths (Crew.kickoff_async/ streaming kickoff), so a loop is always runningChanges
One-line, behavior-identical replacement of
asyncio.get_event_loop()withasyncio.get_running_loop()in each of the three files above.Verification
py_compilepasses for all three filesruff check --no-fixreports no new findings on the changed lines (existing findings in these files are pre-existing and untouched)get_running_loop()returns the same loopget_event_loop()wouldLLM disclosure
This change was prepared with the assistance of an AI coding agent and reviewed before submission. Per the repository's contribution guidelines, please add the
llm-generatedlabel to this PR if appropriate (I don't have permission to add labels myself).Note
Low Risk
Mechanical asyncio API swap in paths that already require a running loop; no auth, data, or business-logic changes.
Overview
Replaces
asyncio.get_event_loop()withasyncio.get_running_loop()in three places that always run inside an active event loop, avoiding Python 3.12+ deprecation and stricter behavior in 3.14 without changing runtime behavior at those call sites.ChromaDB async locking (
client._alocked) still runs cross-process lock enter/exit viarun_in_executor, but now binds to the loop that is already running the coroutine.Async structured tools (
CrewStructuredTool.ainvoke) uses the same pattern when executing a synchronous tool function in a thread pool.Async LLM streaming setup (
create_streaming_statewhenuse_async=True) captures that loop for thread-safecall_soon_threadsafedelivery of stream chunks into anasyncio.Queue.Reviewed by Cursor Bugbot for commit 20d369f. Bugbot is set up for automated code reviews on this repo. Configure here.
Fixes #8912