Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@
from uagents.utils import get_logger, set_global_log_level


def _default_event_loop(
loop: asyncio.AbstractEventLoop | None,
) -> asyncio.AbstractEventLoop:
Comment thread
tejus3131 marked this conversation as resolved.
"""Resolve the event loop for Agent/Bureau construction.

Agent and Bureau need a loop during synchronous __init__ (ASGIServer, create_task).
Historically, asyncio.get_event_loop() implicitly created a main-thread loop when none
existed (Python 3.10-3.13). Python 3.14+ raises RuntimeError instead.

Order: honor explicit loop=, reuse the thread's loop if set, else create one and
register it via set_event_loop() so later get_event_loop() calls on this thread work.
"""
if loop is not None:
return loop
try:
return asyncio.get_event_loop()
except RuntimeError:
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
return new_loop
Comment thread
tejus3131 marked this conversation as resolved.
Comment thread
tejus3131 marked this conversation as resolved.


async def _run_interval(
func: IntervalCallback,
logger: logging.Logger,
Expand Down Expand Up @@ -355,7 +377,8 @@ def __init__(
self._name = name
self._port = port or 8000

self._loop = loop or asyncio.get_event_loop_policy().get_event_loop()
# See _default_event_loop: required at init time, not only in run() (Python 3.14+).
self._loop = _default_event_loop(loop)

# initialize wallet and identity
self._initialize_wallet_and_identity(seed, name, wallet_key_derivation_index)
Expand Down Expand Up @@ -1572,7 +1595,8 @@ def __init__(
log_level (int | str): The logging level for the bureau.
shutdown_timeout (int): The timeout for shutting down the bureau.
"""
self._loop = loop or asyncio.get_event_loop_policy().get_event_loop()
# See _default_event_loop: same Python 3.14+ loop resolution as Agent.
self._loop = _default_event_loop(loop)
self._agents: list[Agent] = []
self._port = port or 8000
self._queries: dict[str, asyncio.Future] = {}
Expand Down
12 changes: 10 additions & 2 deletions python/tests/test_bureau.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@

class TestBureau(unittest.IsolatedAsyncioTestCase):
def setUp(self) -> None:
self.loop = asyncio.get_event_loop()
return super().setUp()
# Obtain a loop before super().setUp() so agents can use loop=self.loop.
# Python 3.14+ no longer auto-creates a loop on get_event_loop(); mirror
# _default_event_loop in agent.py. Do not use get_running_loop() here:
# IsolatedAsyncioTestCase has not started the test loop yet in sync setUp.
try:
self.loop = asyncio.get_event_loop()
except RuntimeError:
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
Comment thread
tejus3131 marked this conversation as resolved.
Outdated
super().setUp()
Comment thread
tejus3131 marked this conversation as resolved.

def test_bureau_updates_agents_no_ledger_batch(self):
alice = Agent(name="alice", endpoint=ALICE_ENDPOINT.url, loop=self.loop)
Expand Down
Loading