fix(uagents): support event loop init on Python 3.14#904
Open
tejus3131 wants to merge 3 commits into
Open
Conversation
Replace deprecated get_event_loop() usage in Agent and Bureau __init__ with _default_event_loop() that creates and registers a thread loop when none exists (Python 3.14+ main thread). When a new loop is created, asyncio.set_event_loop() is called intentionally so later get_event_loop() on that thread succeeds (matches pre-3.14 behavior). Update test_bureau setUp to handle missing thread loop on Python 3.14.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR updates event-loop resolution for Agent/Bureau construction (and a related test) to handle Python 3.14+ behavior where asyncio.get_event_loop() no longer implicitly creates a loop.
Changes:
- Added
_default_event_loop()to centralize loop resolution (reuse existing loop or create+register a new one). - Switched
AgentandBureauconstructors to use_default_event_loop()instead of the event-loop policy default. - Updated
TestBureau.setUp()to pre-resolve an event loop for constructing agents in synchronoussetUp().
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| python/tests/test_bureau.py | Adjusts test setup to acquire/create an event loop on Python 3.14+. |
| python/src/uagents/agent.py | Introduces shared loop-resolution helper and applies it to Agent and Bureau initialization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
get_event_loop() can return a loop closed by Agent.run() still registered on the thread. _default_event_loop() now recreates when missing or closed. - Add TestDefaultEventLoop (explicit, none, closed thread cases) - test_bureau setUp uses _default_event_loop(None)
Avoid leaving a closed loop as the thread default after Agent/Bureau.run(). Complements _default_event_loop is_closed handling. Test harness lint/restore fixes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed Changes
_default_event_loop()inpython/src/uagents/agent.pyand use it inAgentandBureau__init__instead ofget_event_loop_policy().get_event_loop().RuntimeError: There is no current event loop in thread 'MainThread'when constructing agents at module level on Python 3.14+.python/tests/test_bureau.pysetUpto obtain or create a thread loop beforeIsolatedAsyncioTestCasesetup (3.14-safe).Linked Issues
Fixes #903
Types of changes
Checklist
uv run pytestinpython/)If applicable
Test plan
uv run ruff check && uv run ruff formatinpython/pyteston 3.11, 3.12, 3.13 (107 passed each)asyncio.set_event_loop(None)thenAgent(...)succeeds (simulates 3.14 main thread)Further comments
Agent.run()still closesself._loop(unchanged); embedding with a shared external loop is a separate concern (#580).