Skip to content

fix: use running loop in async paths - #5901

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

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

Conversation

@Ghraven

@Ghraven Ghraven commented May 22, 2026

Copy link
Copy Markdown
Contributor

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

coderabbitai Bot commented May 22, 2026

Copy link
Copy Markdown

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.

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open for 45 days with no activity.

@Ghraven

Ghraven commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Hi maintainers, just keeping this from going stale. I’m still happy to revise or close this if the async-loop cleanup is no longer useful. Thanks for all the work on crewAI.

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open for 45 days with no activity.

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for the pull request.

First-time contributors need an associated open issue before we can review a PR.

  1. Open an issue with a template, or pick an existing open one.
  2. Open a new PR (or reopen this one) whose title or body mentions that issue, for example #123.

See the contributing guide.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants