From 122b48a57f9a49700d9cd1d5c3a7ca4a4c340563 Mon Sep 17 00:00:00 2001 From: Hemanth Nakkina Date: Mon, 30 Nov 2026 10:57:11 +0530 Subject: [PATCH] fix: create event-loop on Python 3.14 where get_event_loop raises Python 3.14 removed implicit event-loop creation in the main thread; asyncio.get_event_loop() now raises RuntimeError when no loop is set. Wrap loop lookup in _get_or_create_event_loop() so synchronous use of the client (Asynchronous metaclass, is_loop_running) keeps working: fall back to creating and setting a new loop, matching the behaviour of Python <= 3.12. --- maas/client/utils/maas_async.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/maas/client/utils/maas_async.py b/maas/client/utils/maas_async.py index 19b8bc31..2965d4b9 100644 --- a/maas/client/utils/maas_async.py +++ b/maas/client/utils/maas_async.py @@ -16,11 +16,27 @@ __all__ = ["asynchronous", "Asynchronous", "is_loop_running"] -from asyncio import get_event_loop +from asyncio import get_event_loop, new_event_loop, set_event_loop from functools import wraps from inspect import isawaitable, iscoroutinefunction +def _get_or_create_event_loop(): + """Return the current event-loop, creating one if necessary. + + Python 3.14 removed the implicit creation of an event-loop in the main + thread, so `get_event_loop()` raises `RuntimeError` when no loop has been + set. Fall back to creating and setting a new loop, matching the previous + behaviour on older Python versions. + """ + try: + return get_event_loop() + except RuntimeError: + eventloop = new_event_loop() + set_event_loop(eventloop) + return eventloop + + def asynchronous(func): """Return `func` in a "smart" asynchronous-aware wrapper. @@ -36,7 +52,7 @@ def asynchronous(func): @wraps(func) def wrapper(*args, **kwargs): - eventloop = get_event_loop() + eventloop = _get_or_create_event_loop() result = func(*args, **kwargs) if not eventloop.is_running(): while isawaitable(result): @@ -74,4 +90,4 @@ def is_loop_running(): Then we don't want to block, do we? """ - return get_event_loop().is_running() + return _get_or_create_event_loop().is_running()