[PATCH 01/14] python: Replace asyncio.get_event_loop for Python 3.14

Daniel P. Berrangé posted 14 patches 4 months ago
Maintainers: John Snow <jsnow@redhat.com>, Cleber Rosa <crosa@redhat.com>, Thomas Huth <thuth@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, "Daniel P. Berrangé" <berrange@redhat.com>, Elena Ufimtseva <elena.ufimtseva@oracle.com>, Jagannathan Raman <jag.raman@oracle.com>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>
[PATCH 01/14] python: Replace asyncio.get_event_loop for Python 3.14
Posted by Daniel P. Berrangé 4 months ago
From: Richard W.M. Jones <rjones@redhat.com>

In Python 3.14, no asyncio event loop gets generated automatically.
Instead create one when we need it.  This should work with Python 3.13
as well.  This change was suggested here:

https://bugzilla.redhat.com/show_bug.cgi?id=2375004#c4

See-also: https://docs.python.org/3.14/whatsnew/3.14.html#id7
Thanks: Miro Hrončok, Daniel P. Berrangé
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
---
 python/qemu/qmp/legacy.py  | 5 ++++-
 python/qemu/qmp/qmp_tui.py | 2 +-
 python/tests/protocol.py   | 2 +-
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/python/qemu/qmp/legacy.py b/python/qemu/qmp/legacy.py
index 22a2b5616e..e11d05afbd 100644
--- a/python/qemu/qmp/legacy.py
+++ b/python/qemu/qmp/legacy.py
@@ -86,7 +86,10 @@ def __init__(self,
                 "server argument should be False when passing a socket")
 
         self._qmp = QMPClient(nickname)
-        self._aloop = asyncio.get_event_loop()
+        try:
+            self._aloop = asyncio.get_event_loop()
+        except RuntimeError:
+            self._aloop = asyncio.new_event_loop()
         self._address = address
         self._timeout: Optional[float] = None
 
diff --git a/python/qemu/qmp/qmp_tui.py b/python/qemu/qmp/qmp_tui.py
index 2d9ebbd20b..7dfb03c9ad 100644
--- a/python/qemu/qmp/qmp_tui.py
+++ b/python/qemu/qmp/qmp_tui.py
@@ -377,7 +377,7 @@ def run(self, debug: bool = False) -> None:
         screen = urwid.raw_display.Screen()
         screen.set_terminal_properties(256)
 
-        self.aloop = asyncio.get_event_loop()
+        self.aloop = asyncio.new_event_loop()
         self.aloop.set_debug(debug)
 
         # Gracefully handle SIGTERM and SIGINT signals
diff --git a/python/tests/protocol.py b/python/tests/protocol.py
index 56c4d441f9..db5d54d83f 100644
--- a/python/tests/protocol.py
+++ b/python/tests/protocol.py
@@ -228,7 +228,7 @@ def async_test(async_test_method):
         Decorator; adds SetUp and TearDown to async tests.
         """
         async def _wrapper(self, *args, **kwargs):
-            loop = asyncio.get_event_loop()
+            loop = asyncio.new_event_loop()
             loop.set_debug(True)
 
             await self._asyncSetUp()
-- 
2.49.0


Re: [PATCH 01/14] python: Replace asyncio.get_event_loop for Python 3.14
Posted by John Snow 2 months, 3 weeks ago
On Tue, Jul 15, 2025 at 10:30 AM Daniel P. Berrangé <berrange@redhat.com> wrote:
>
> From: Richard W.M. Jones <rjones@redhat.com>
>
> In Python 3.14, no asyncio event loop gets generated automatically.
> Instead create one when we need it.  This should work with Python 3.13
> as well.  This change was suggested here:
>
> https://bugzilla.redhat.com/show_bug.cgi?id=2375004#c4
>
> See-also: https://docs.python.org/3.14/whatsnew/3.14.html#id7
> Thanks: Miro Hrončok, Daniel P. Berrangé
> Signed-off-by: Richard W.M. Jones <rjones@redhat.com>

Reviewed-by: John Snow <jsnow@redhat.com>

We fixed this in the standalone tree, but I hadn't synchronized the
changes over yet. Shall we take your patches first, and then I'll
worry about synchronizing later?

(I desperately need to drop this code from the qemu.git tree, urgh.)

> ---
>  python/qemu/qmp/legacy.py  | 5 ++++-
>  python/qemu/qmp/qmp_tui.py | 2 +-
>  python/tests/protocol.py   | 2 +-
>  3 files changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/python/qemu/qmp/legacy.py b/python/qemu/qmp/legacy.py
> index 22a2b5616e..e11d05afbd 100644
> --- a/python/qemu/qmp/legacy.py
> +++ b/python/qemu/qmp/legacy.py
> @@ -86,7 +86,10 @@ def __init__(self,
>                  "server argument should be False when passing a socket")
>
>          self._qmp = QMPClient(nickname)
> -        self._aloop = asyncio.get_event_loop()
> +        try:
> +            self._aloop = asyncio.get_event_loop()
> +        except RuntimeError:
> +            self._aloop = asyncio.new_event_loop()
>          self._address = address
>          self._timeout: Optional[float] = None
>
> diff --git a/python/qemu/qmp/qmp_tui.py b/python/qemu/qmp/qmp_tui.py
> index 2d9ebbd20b..7dfb03c9ad 100644
> --- a/python/qemu/qmp/qmp_tui.py
> +++ b/python/qemu/qmp/qmp_tui.py
> @@ -377,7 +377,7 @@ def run(self, debug: bool = False) -> None:
>          screen = urwid.raw_display.Screen()
>          screen.set_terminal_properties(256)
>
> -        self.aloop = asyncio.get_event_loop()
> +        self.aloop = asyncio.new_event_loop()
>          self.aloop.set_debug(debug)
>
>          # Gracefully handle SIGTERM and SIGINT signals
> diff --git a/python/tests/protocol.py b/python/tests/protocol.py
> index 56c4d441f9..db5d54d83f 100644
> --- a/python/tests/protocol.py
> +++ b/python/tests/protocol.py
> @@ -228,7 +228,7 @@ def async_test(async_test_method):
>          Decorator; adds SetUp and TearDown to async tests.
>          """
>          async def _wrapper(self, *args, **kwargs):
> -            loop = asyncio.get_event_loop()
> +            loop = asyncio.new_event_loop()
>              loop.set_debug(True)
>
>              await self._asyncSetUp()
> --
> 2.49.0
>
Re: [PATCH 01/14] python: Replace asyncio.get_event_loop for Python 3.14
Posted by Daniel P. Berrangé 2 months, 3 weeks ago
On Tue, Aug 19, 2025 at 03:47:50PM -0400, John Snow wrote:
> On Tue, Jul 15, 2025 at 10:30 AM Daniel P. Berrangé <berrange@redhat.com> wrote:
> >
> > From: Richard W.M. Jones <rjones@redhat.com>
> >
> > In Python 3.14, no asyncio event loop gets generated automatically.
> > Instead create one when we need it.  This should work with Python 3.13
> > as well.  This change was suggested here:
> >
> > https://bugzilla.redhat.com/show_bug.cgi?id=2375004#c4
> >
> > See-also: https://docs.python.org/3.14/whatsnew/3.14.html#id7
> > Thanks: Miro Hrončok, Daniel P. Berrangé
> > Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
> 
> Reviewed-by: John Snow <jsnow@redhat.com>
> 
> We fixed this in the standalone tree, but I hadn't synchronized the
> changes over yet. Shall we take your patches first, and then I'll
> worry about synchronizing later?

This really needs to be in 10.1.0, because tests crash and
burn under python 3.14 without this - we're applying this
in Fedora builds locally to deal with the brokeness.

> (I desperately need to drop this code from the qemu.git tree, urgh.)

IMHO qemu.git should be considered authoritative right now because
that's what we're actually shipping & thus take priority for merge.

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Re: [PATCH 01/14] python: Replace asyncio.get_event_loop for Python 3.14
Posted by John Snow 2 months, 3 weeks ago
On Tue, Aug 19, 2025 at 3:51 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
>
> On Tue, Aug 19, 2025 at 03:47:50PM -0400, John Snow wrote:
> > On Tue, Jul 15, 2025 at 10:30 AM Daniel P. Berrangé <berrange@redhat.com> wrote:
> > >
> > > From: Richard W.M. Jones <rjones@redhat.com>
> > >
> > > In Python 3.14, no asyncio event loop gets generated automatically.
> > > Instead create one when we need it.  This should work with Python 3.13
> > > as well.  This change was suggested here:
> > >
> > > https://bugzilla.redhat.com/show_bug.cgi?id=2375004#c4
> > >
> > > See-also: https://docs.python.org/3.14/whatsnew/3.14.html#id7
> > > Thanks: Miro Hrončok, Daniel P. Berrangé
> > > Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
> >
> > Reviewed-by: John Snow <jsnow@redhat.com>
> >
> > We fixed this in the standalone tree, but I hadn't synchronized the
> > changes over yet. Shall we take your patches first, and then I'll
> > worry about synchronizing later?
>
> This really needs to be in 10.1.0, because tests crash and
> burn under python 3.14 without this - we're applying this
> in Fedora builds locally to deal with the brokeness.

Urk. I didn't realize we were waiting on this; please include it with
my blessings and apologies.

>
> > (I desperately need to drop this code from the qemu.git tree, urgh.)
>
> IMHO qemu.git should be considered authoritative right now because
> that's what we're actually shipping & thus take priority for merge.

Yup, you're right. I just mean I need to stop the duplication, because
stuff like this happens.

>
> With regards,
> Daniel
> --
> |: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
> |: https://libvirt.org         -o-            https://fstop138.berrange.com :|
> |: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
>