Skip to content

fix(uagents): support event loop init on Python 3.14#904

Open
tejus3131 wants to merge 3 commits into
fetchai:mainfrom
tejus3131:fix/uagents-python-3.14-event-loop
Open

fix(uagents): support event loop init on Python 3.14#904
tejus3131 wants to merge 3 commits into
fetchai:mainfrom
tejus3131:fix/uagents-python-3.14-event-loop

Conversation

@tejus3131
Copy link
Copy Markdown

@tejus3131 tejus3131 commented Jun 4, 2026

Proposed Changes

  • Add _default_event_loop() in python/src/uagents/agent.py and use it in Agent and Bureau __init__ instead of get_event_loop_policy().get_event_loop().
  • Fixes RuntimeError: There is no current event loop in thread 'MainThread' when constructing agents at module level on Python 3.14+.
  • Update python/tests/test_bureau.py setUp to obtain or create a thread loop before IsolatedAsyncioTestCase setup (3.14-safe).

Linked Issues

Fixes #903

Types of changes

  • Bug fix (non-breaking change that fixes an issue).
  • New feature added
  • Breaking change
  • Documentation update
  • Something else

Checklist

  • I have read the CONTRIBUTING guide
  • Checks and tests pass locally on Python 3.11, 3.12, and 3.13 (uv run pytest in python/)

If applicable

  • I have added tests that prove my fix is effective (existing suite + bureau harness fix; no new test class in this PR)
  • I have added/updated the documentation

Test plan

  • uv run ruff check && uv run ruff format in python/
  • Full pytest on 3.11, 3.12, 3.13 (107 passed each)
  • Manual repro: asyncio.set_event_loop(None) then Agent(...) succeeds (simulates 3.14 main thread)

Further comments

  • Agent.run() still closes self._loop (unchanged); embedding with a shared external loop is a separate concern (#580).

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.
Copilot AI review requested due to automatic review settings June 4, 2026 12:40
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Agent and Bureau constructors 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 synchronous setUp().

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.

Comment thread python/src/uagents/agent.py
Comment thread python/tests/test_bureau.py Outdated
Comment thread python/src/uagents/agent.py
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)
@tejus3131 tejus3131 requested a review from Copilot June 4, 2026 13:10
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread python/src/uagents/agent.py
Comment thread python/tests/test_bureau.py
Comment thread python/tests/test_agent.py Outdated
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.
@tejus3131 tejus3131 requested a review from Copilot June 4, 2026 13:39
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread python/tests/test_bureau.py
Comment thread python/tests/test_agent.py
Comment thread python/src/uagents/agent.py
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.

Agent and Bureau fail to construct on Python 3.14 (asyncio.get_event_loop)

2 participants