Skip to content

fix: replace deprecated asyncio.get_event_loop() with get_running_loop()#5969

Open
kratos0718 wants to merge 1 commit into
crewAIInc:mainfrom
kratos0718:fix/replace-deprecated-asyncio-get-event-loop
Open

fix: replace deprecated asyncio.get_event_loop() with get_running_loop()#5969
kratos0718 wants to merge 1 commit into
crewAIInc:mainfrom
kratos0718:fix/replace-deprecated-asyncio-get-event-loop

Conversation

@kratos0718
Copy link
Copy Markdown

@kratos0718 kratos0718 commented May 29, 2026

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.pyCrewStructuredTool._arun falls back to running a sync self.func in the executor when the function is not a coroutine. Also removed a redundant inline import asyncio since the module is already imported at the top of the file.

  • crewai_tools/tools/snowflake_search_tool/snowflake_search_tool.pySnowflakeSearchTool._get_connection runs self._create_connection in 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.

Review Change Stack

…loop()`

`asyncio.get_event_loop()` is deprecated when there is no running event
loop and emits a `DeprecationWarning` from Python 3.10 onwards. 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._arun` falls
  back to running a sync `self.func` in the executor. Also removed a
  redundant inline `import asyncio` since the module is already imported
  at the top of the file.

- `crewai_tools/tools/snowflake_search_tool/snowflake_search_tool.py` —
  `SnowflakeSearchTool._get_connection` runs `self._create_connection`
  in the executor when no pooled connection is available.

No behavior change. `run_in_executor` semantics are identical between
the two loop accessors when a running loop is present; the only
difference is the deprecation contract.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 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: af0bfd92-7a4a-4f6e-ab89-1e37f9669b9a

📥 Commits

Reviewing files that changed from the base of the PR and between fca21b1 and 5f4badf.

📒 Files selected for processing (2)
  • lib/crewai-tools/src/crewai_tools/tools/snowflake_search_tool/snowflake_search_tool.py
  • lib/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 review command to re-trigger a full review. If the issue persists, set path_filters to 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.

❤️ Share

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