fix: replace deprecated `asyncio.get_event_loop()` with `get_running_loop()`
Summary
asyncio.get_event_loop() is deprecated when there is no running event loop and emits a DeprecationWarning from Python 3.10 onwards. Per the official Python docs:
Deprecated since version 3.10: Deprecation warning is emitted if there is no current event loop. In future versions of Python this will be raised as a RuntimeError.
Both call sites in this fix are inside async def coroutines that are guaranteed to have a running loop, so asyncio.get_running_loop() is the correct replacement.
Affected call sites
-
crewai/tools/structured_tool.py—CrewStructuredTool._arunfalls back to running a syncself.funcin the executor when the function is not a coroutine. Also removed a redundant inlineimport asynciosince the module is already imported at the top of the file. -
crewai_tools/tools/snowflake_search_tool/snowflake_search_tool.py—SnowflakeSearchTool._get_connectionrunsself._create_connectionin the executor when no pooled connection is available.
Behavior
No behavior change. run_in_executor semantics are identical between the two loop accessors when a running loop is present (which is the case in both contexts here, since both are inside async def and the surrounding code already uses await). The only difference is the deprecation contract.
Note
I noticed this is the same pattern that was fixed in agno PR #8111 — keeping crewAI ready for Python versions that promote the warning to an error.
Summary by CodeRabbit
- Bug Fixes
- Improved reliability of async operations in tool execution and database connections through enhanced event loop handling.
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: af0bfd92-7a4a-4f6e-ab89-1e37f9669b9a
📥 Commits
Reviewing files that changed from the base of the PR and between fca21b155c4f316ee63d4aa1725361aff392e47e and 5f4badfdbc84f54696170a47028e284074430629.
📒 Files selected for processing (2)
lib/crewai-tools/src/crewai_tools/tools/snowflake_search_tool/snowflake_search_tool.pylib/crewai/src/crewai/tools/structured_tool.py
📝 Walkthrough
Walkthrough
Two async methods are updated to use asyncio.get_running_loop() instead of asyncio.get_event_loop() when scheduling synchronous executor calls. SnowflakeSearchTool._get_connection and CrewStructuredTool.ainvoke both adopt this pattern to ensure correct event loop handling within async contexts.
Changes
Event loop accessor migration
| Layer / File(s) | Summary |
|---|---|
Migrate to get_running_loop in async executor scheduling lib/crewai-tools/src/crewai_tools/tools/snowflake_search_tool/snowflake_search_tool.py, lib/crewai/src/crewai/tools/structured_tool.py |
SnowflakeSearchTool._get_connection and CrewStructuredTool.ainvoke both replace asyncio.get_event_loop() with asyncio.get_running_loop() when scheduling synchronous thread-pool executor work, preserving the same callable behavior while fixing event loop acquisition. |
Estimated code review effort
🎯 1 (Trivial) | ⏱️ ~3 minutes
Poem
🐰 A loop runs true, we took the wheel, From old to new with async zeal, Two paths align, one pattern clear, Running loops through executors here! ✨
🚥 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 accurately describes the main change: replacing deprecated asyncio.get_event_loop() with get_running_loop() in async contexts across two files. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
| 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
[!WARNING]
Review ran into problems
🔥 Problems
Git: Failed to clone repository. Please run the
@coderabbitai full reviewcommand to re-trigger a full review. If the issue persists, setpath_filtersto include or exclude specific files.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.