Skip to content

fix: use running loop in async paths#5901

Open
Ghraven wants to merge 1 commit into
crewAIInc:mainfrom
Ghraven:fix/use-running-loop-in-async-paths
Open

fix: use running loop in async paths#5901
Ghraven wants to merge 1 commit into
crewAIInc:mainfrom
Ghraven:fix/use-running-loop-in-async-paths

Conversation

@Ghraven
Copy link
Copy Markdown

@Ghraven Ghraven commented May 22, 2026

Summary

  • Replace asyncio.get_event_loop() with asyncio.get_running_loop() in async execution paths
  • Cover tool executor, streaming state setup, and ChromaDB async lock code paths

Problem

asyncio.get_event_loop() is legacy behavior in modern Python and can warn or behave differently when no loop is set on the current thread. These call sites are already inside async execution paths, so the running event loop is the loop they need.

Before / After

Before, async code asked asyncio for the current event loop policy result with get_event_loop().

After, the code explicitly uses the active running loop with get_running_loop(), matching the async context these functions run in.

Verification

  • python -m compileall -q lib\crewai\src\crewai\tools\structured_tool.py lib\crewai\src\crewai\utilities\streaming.py lib\crewai\src\crewai\rag\chromadb\client.py
  • git diff --check

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of async operations throughout the framework by fixing event loop context handling in RAG operations, structured tool execution, and streaming functionality for better stability in concurrent scenarios.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e3fdd41f-fefe-4c36-9f03-b02a89bbd4e0

📥 Commits

Reviewing files that changed from the base of the PR and between 179c20b and 06aa50e.

📒 Files selected for processing (3)
  • lib/crewai/src/crewai/rag/chromadb/client.py
  • lib/crewai/src/crewai/tools/structured_tool.py
  • lib/crewai/src/crewai/utilities/streaming.py

📝 Walkthrough

Walkthrough

This PR updates three async execution paths to use asyncio.get_running_loop() instead of asyncio.get_event_loop() when scheduling work via executor or initializing async contexts. Changes affect ChromaDB client locks, structured tool invocation, and streaming state initialization without modifying signatures or public APIs.

Changes

Async Event Loop Context Refactor

Layer / File(s) Summary
Event loop acquisition via asyncio.get_running_loop()
lib/crewai/src/crewai/rag/chromadb/client.py, lib/crewai/src/crewai/tools/structured_tool.py, lib/crewai/src/crewai/utilities/streaming.py
ChromaDBClient._alocked acquires the loop for executor-based lock scheduling; CrewStructuredTool.ainvoke acquires the loop for thread-pool execution of sync tool functions; create_streaming_state acquires the loop when initializing async streaming. All three now use asyncio.get_running_loop() to retrieve the active event loop context.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 Three loops now run in async's way,
No more searching for loops astray,
Get the running one, we say!
Streaming, tools, and locks align,
In async context, all is fine! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: use running loop in async paths' accurately and concisely describes the main change across all three modified files, clearly summarizing the primary objective of replacing asyncio.get_event_loop() with asyncio.get_running_loop().
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant