python/qemu/machine: fix potential hang in QMP accept
v2 cover python/qemu/machine: fix potential hang in QMP accept
From: Marc-André Lureau <[email protected]>
Hi,
As reported earlier by Richard Henderson ("virgl avocado hang" thread), avocado tests may hang when QEMU exits before the QMP connection is established.
v2:
- use a socketpair() for QMP (instead of async concurrent code from v1) as suggested by Daniel Berrange.
- should not regress (hopefully)
Marc-André Lureau (3):
python/qmp/protocol: add open_with_socket() python/qmp/legacy: make QEMUMonitorProtocol accept a socket python/qemu/machine: use socketpair() for QMP by default
python/qemu/machine/machine.py | 24 ++++++++++++++++-------- python/qemu/qmp/legacy.py | 18 +++++++++++++++--- python/qemu/qmp/protocol.py | 25 ++++++++++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-)
Signature · 1 line
2.37.0.rc0
PATCH v2 1/3 python/qmp/protocol: add open_with_socket()
From: Marc-André Lureau <[email protected]>
Instead of listening for incoming connections with a SocketAddr, add a new method open_with_socket() that accepts an existing socket.
--- python/qemu/qmp/protocol.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
python/qemu/qmp/protocol.py +20 -5
@@ -18,6 +18,7 @@ from enum import Enum from functools import wraps import logging +import socket from ssl import SSLContext from typing import ( Any,
@@ -296,6 +297,19 @@ async def start_server_and_accept( await self.accept() assert self.runstate == Runstate.RUNNING + @upper_half + @require(Runstate.IDLE) + async def open_with_socket(self, sock: socket.socket) -> None: + """ + Start connection with given socket. + + :param sock: A socket. + + :raise StateError: When the `Runstate` is not `IDLE`. + """ + self._reader, self._writer = await asyncio.open_connection(sock=sock) + self._set_state(Runstate.CONNECTING) + @upper_half @require(Runstate.IDLE) async def start_server(self, address: SocketAddrT,
@@ -343,11 +357,12 @@ async def accept(self) -> None: protocol-level failure occurs while establishing a new session, the wrapped error may also be an `QMPError`. """ - if self._accepted is None: - raise QMPError("Cannot call accept() before start_server().") - await self._session_guard( - self._do_accept(), - 'Failed to establish connection') + if not self._reader: + if self._accepted is None: + raise QMPError("Cannot call accept() before start_server().") + await self._session_guard( + self._do_accept(), + 'Failed to establish connection') await self._session_guard( self._establish_session(), 'Failed to establish session')
Signature · 1 line
2.37.0.rc0
PATCH v2 2/3 python/qmp/legacy: make QEMUMonitorProtocol accept a socket
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Teach QEMUMonitorProtocol to accept an exisiting socket.
--- python/qemu/qmp/legacy.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
python/qemu/qmp/legacy.py +15 -3
@@ -22,6 +22,7 @@ # import asyncio +import socket from types import TracebackType from typing import ( Any,
@@ -69,22 +70,32 @@ class QEMUMonitorProtocol: :param address: QEMU address, can be either a unix socket path (string) or a tuple in the form ( address, port ) for a TCP - connection + connection or None + :param sock: a socket or None :param server: Act as the socket server. (See 'accept') :param nickname: Optional nickname used for logging. """ - def __init__(self, address: SocketAddrT, + def __init__(self, + address: Optional[SocketAddrT] = None, + sock: Optional[socket.socket] = None, server: bool = False, nickname: Optional[str] = None): + assert address or sock self._qmp = QMPClient(nickname) self._aloop = asyncio.get_event_loop() self._address = address + self._sock = sock self._timeout: Optional[float] = None if server: - self._sync(self._qmp.start_server(self._address)) + if sock: + assert self._sock is not None + self._sync(self._qmp.open_with_socket(self._sock)) + else: + assert self._address is not None + self._sync(self._qmp.start_server(self._address)) _T = TypeVar('_T')
@@ -139,6 +150,7 @@ def connect(self, negotiate: bool = True) -> Optional[QMPMessage]: :return: QMP greeting dict, or None if negotiate is false :raise ConnectError: on connection errors """ + assert self._address is not None self._qmp.await_greeting = negotiate self._qmp.negotiate = negotiate
Signature · 1 line
2.37.0.rc0
PATCH v2 3/3 python/qemu/machine: use socketpair() for QMP by default
In reply to [email protected]From: Marc-André Lureau <[email protected]>
When no monitor address is given, establish the QMP communication through a socketpair() (API is also supported on Windows since Python 3.5)
--- python/qemu/machine/machine.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
python/qemu/machine/machine.py +16 -8
@@ -158,17 +158,13 @@ def __init__(self, self._qmp_timer = qmp_timer self._name = name or f"qemu-{os.getpid()}-{id(self):02x}" + self._sock_pair: Optional[Tuple[socket.socket, socket.socket]] = None self._temp_dir: Optional[str] = None self._base_temp_dir = base_temp_dir self._sock_dir = sock_dir self._log_dir = log_dir - if monitor_address is not None: - self._monitor_address = monitor_address - else: - self._monitor_address = os.path.join( - self.sock_dir, f"{self._name}-monitor.sock" - ) + self._monitor_address = monitor_address self._console_log_path = console_log if self._console_log_path:
@@ -303,7 +299,11 @@ def _base_args(self) -> List[str]: args = ['-display', 'none', '-vga', 'none'] if self._qmp_set: - if isinstance(self._monitor_address, tuple): + if self._sock_pair: + fd = self._sock_pair[0].fileno() + os.set_inheritable(fd, True) + moncdev = f"socket,id=mon,fd={fd}" + elif isinstance(self._monitor_address, tuple): moncdev = "socket,id=mon,host={},port={}".format( *self._monitor_address )
@@ -337,10 +337,17 @@ def _pre_launch(self) -> None: self._remove_files.append(self._console_address) if self._qmp_set: + monitor_address = None + sock = None + if self._monitor_address is None: + self._sock_pair = socket.socketpair() + sock = self._sock_pair[1] if isinstance(self._monitor_address, str): self._remove_files.append(self._monitor_address) + monitor_address = self._monitor_address self._qmp_connection = QEMUMonitorProtocol( - self._monitor_address, + address=monitor_address, + sock=sock, server=True, nickname=self._name )
@@ -360,6 +367,7 @@ def _pre_launch(self) -> None: )) def _post_launch(self) -> None: + self._sock_pair[0].close() if self._qmp_connection: self._qmp.accept(self._qmp_timer)
Signature · 1 line
2.37.0.rc0
Re: [PATCH v2 3/3] python/qemu/machine: use socketpair() for QMP by default
Quoted 21 lines · On Thu, Jun 30, 2022 at 04:34:19PM +0400, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
When no monitor address is given, establish the QMP communication through a
socketpair()(API is also supported on Windows since Python 3.5)Signed-off-by Marc-André Lureau <[email protected]>--- python/qemu/machine/machine.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)python/qemu/machine/machine.py +1
-0@@ -158,17 +158,13 @@ def __init__(self, self._qmp_timer = qmp_timer self._name = name or f"qemu-{os.getpid()}-{id(self):02x}" + self._sock_pair: Optional[Tuple[socket.socket, socket.socket]] = None self._temp_dir: Optional[str] = None self._base_temp_dir = base_temp_dir
python/qemu/machine/machine.py def __init__(self, · line 167
Quoted 10 lines
python/qemu/machine/machine.py +1
-6self._sock_dir = sock_dir self._log_dir = log_dir - if monitor_address is not None: - self._monitor_address = monitor_address - else: - self._monitor_address = os.path.join( - self.sock_dir, f"{self._name}-monitor.sock" - ) + self._monitor_address = monitor_address
Almost nothing in QEMU passes 'monitor_address' right now, so thue effect of this will be that essentially all usage switches to the socketpair behaviour. Should be ok, as nothing is expecting to have the ability to leave QEMU running, and re-connect to its monitor in another process later.
Quoted 46 lines
python/qemu/machine/machine.py +14
-2self._console_log_path = console_log if self._console_log_path:@@ -303,7 +299,11 @@ def _base_args(self) -> List[str]: args = ['-display', 'none', '-vga', 'none'] if self._qmp_set: - if isinstance(self._monitor_address, tuple): + if self._sock_pair: + fd = self._sock_pair[0].fileno() + os.set_inheritable(fd, True) + moncdev = f"socket,id=mon,fd={fd}" + elif isinstance(self._monitor_address, tuple): moncdev = "socket,id=mon,host={},port={}".format( *self._monitor_address )@@ -337,10 +337,17 @@ def _pre_launch(self) -> None: self._remove_files.append(self._console_address) if self._qmp_set: + monitor_address = None + sock = None + if self._monitor_address is None: + self._sock_pair = socket.socketpair() + sock = self._sock_pair[1] if isinstance(self._monitor_address, str): self._remove_files.append(self._monitor_address) + monitor_address = self._monitor_address self._qmp_connection = QEMUMonitorProtocol( - self._monitor_address, + address=monitor_address, + sock=sock, server=True, nickname=self._name )@@ -360,6 +367,7 @@ def _pre_launch(self) -> None: )) def _post_launch(self) -> None: + self._sock_pair[0].close() if self._qmp_connection: self._qmp.accept(self._qmp_timer)Signature · 1 line
2.37.0.rc0
With regards,
Daniel
Signature · 3 lines
|: 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 :|
Quoted 18 lines · On Thu, Jun 30, 2022 at 8:34 AM <[email protected]> wrote:
From: Marc-André Lureau <[email protected]>
Hi,
As reported earlier by Richard Henderson ("virgl avocado hang" thread), avocado tests may hang when QEMU exits before the QMP connection is established.
v2:
- use asocketpair()for QMP (instead of async concurrent code from v1) as suggested by Daniel Berrange.
- should not regress (hopefully)Marc-André Lureau (3):
python/qmp/protocol: addopen_with_socket()python/qmp/legacy: make QEMUMonitorProtocol accept a socket python/qemu/machine: usesocketpair()for QMP by default
Quoted 8 lines
python/qemu/machine/machine.py | 24 ++++++++++++++++-------- python/qemu/qmp/legacy.py | 18 +++++++++++++++--- python/qemu/qmp/protocol.py | 25 ++++++++++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-)--
2.37.0.rc0
For anything that touches python/qemu/qmp/*, may I please ask that you submit them to https://gitlab.com/qemu-project/python-qemu-qmp ?
(I'll review them in the meantime on-list just in the interest of moving things along.)
--js
Hi
Quoted 26 lines · John Snow
Quoted 6 lines · On Thu, Jun 30, 2022 at 8:34 AM <[email protected]> wrote:
From: Marc-André Lureau <[email protected]>
Hi,
As reported earlier by Richard Henderson ("virgl avocado hang" thread),
avocado
Quoted 4 lines
tests may hang when QEMU exits before the QMP connection is established.
v2:
- use asocketpair()for QMP (instead of async concurrent code from v1)as
Quoted 13 lines
suggested by Daniel Berrange.
- should not regress (hopefully)Marc-André Lureau (3):
python/qmp/protocol: addopen_with_socket()python/qmp/legacy: make QEMUMonitorProtocol accept a socket python/qemu/machine: usesocketpair()for QMP by defaultpython/qemu/machine/machine.py | 24 ++++++++++++++++-------- python/qemu/qmp/legacy.py | 18 +++++++++++++++--- python/qemu/qmp/protocol.py | 25 ++++++++++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-)
Quoted 8 lines
Quoted 3 lines
--
2.37.0.rc0For anything that touches python/qemu/qmp/*, may I please ask that you submit them to https://gitlab.com/qemu-project/python-qemu-qmp ?
Ok
Quoted 3 lines
(I'll review them in the meantime on-list just in the interest of moving things along.)
I was waiting for a review before updating the patches / moving to python-qemu-qmp.
thanks
Signature · 1 line
Marc-André Lureau
Daniel P. Berrangé
Reviewed-byQuoted 41 lines · Marc-André Lureau
Hi
Quoted 26 lines · John Snow
Quoted 6 lines · On Thu, Jun 30, 2022 at 8:34 AM <[email protected]> wrote:
From: Marc-André Lureau <[email protected]>
Hi,
As reported earlier by Richard Henderson ("virgl avocado hang" thread),
avocado
Quoted 4 lines
tests may hang when QEMU exits before the QMP connection is established.
v2:
- use asocketpair()for QMP (instead of async concurrent code from v1)as
Quoted 13 lines
suggested by Daniel Berrange.
- should not regress (hopefully)Marc-André Lureau (3):
python/qmp/protocol: addopen_with_socket()python/qmp/legacy: make QEMUMonitorProtocol accept a socket python/qemu/machine: usesocketpair()for QMP by defaultpython/qemu/machine/machine.py | 24 ++++++++++++++++-------- python/qemu/qmp/legacy.py | 18 +++++++++++++++--- python/qemu/qmp/protocol.py | 25 ++++++++++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-)Quoted 8 lines
Quoted 3 lines
--
2.37.0.rc0For anything that touches python/qemu/qmp/*, may I please ask that you submit them to https://gitlab.com/qemu-project/python-qemu-qmp ?
Ok
Quoted 6 lines
Quoted 3 lines
(I'll review them in the meantime on-list just in the interest of moving things along.)
I was waiting for a review before updating the patches / moving to python-qemu-qmp.
This code looks decent to me
With regards,
Daniel
Signature · 3 lines
|: 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 :|
Quoted 37 lines · Marc-André Lureau
Hi
Quoted 25 lines · John Snow
Quoted 23 lines · On Thu, Jun 30, 2022 at 8:34 AM <[email protected]> wrote:
From: Marc-André Lureau <[email protected]>
Hi,
As reported earlier by Richard Henderson ("virgl avocado hang" thread), avocado tests may hang when QEMU exits before the QMP connection is established.
v2:
- use asocketpair()for QMP (instead of async concurrent code from v1) as suggested by Daniel Berrange.
- should not regress (hopefully)Marc-André Lureau (3):
python/qmp/protocol: addopen_with_socket()python/qmp/legacy: make QEMUMonitorProtocol accept a socket python/qemu/machine: usesocketpair()for QMP by defaultpython/qemu/machine/machine.py | 24 ++++++++++++++++-------- python/qemu/qmp/legacy.py | 18 +++++++++++++++--- python/qemu/qmp/protocol.py | 25 ++++++++++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-)Quoted 7 lines
Quoted 3 lines
--
2.37.0.rc0For anything that touches python/qemu/qmp/*, may I please ask that you submit them to https://gitlab.com/qemu-project/python-qemu-qmp ?
Quoted 9 lines
Ok
Quoted 3 lines
(I'll review them in the meantime on-list just in the interest of moving things along.)
I was waiting for a review before updating the patches / moving to python-qemu-qmp.
Fair enough - can I kindly ask you to resend, though? My apologies and Happy New Year!
--js
Hi John
Quoted 47 lines · John Snow
Quoted 44 lines · Marc-André Lureau
Hi
Quoted 25 lines · John Snow
Quoted 23 lines · On Thu, Jun 30, 2022 at 8:34 AM <[email protected]> wrote:
From: Marc-André Lureau <[email protected]>
Hi,
As reported earlier by Richard Henderson ("virgl avocado hang" thread), avocado tests may hang when QEMU exits before the QMP connection is established.
v2:
- use asocketpair()for QMP (instead of async concurrent code from v1) as suggested by Daniel Berrange.
- should not regress (hopefully)Marc-André Lureau (3):
python/qmp/protocol: addopen_with_socket()python/qmp/legacy: make QEMUMonitorProtocol accept a socket python/qemu/machine: usesocketpair()for QMP by defaultpython/qemu/machine/machine.py | 24 ++++++++++++++++-------- python/qemu/qmp/legacy.py | 18 +++++++++++++++--- python/qemu/qmp/protocol.py | 25 ++++++++++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-)Quoted 7 lines
Quoted 3 lines
--
2.37.0.rc0For anything that touches python/qemu/qmp/*, may I please ask that you submit them to https://gitlab.com/qemu-project/python-qemu-qmp ?
Ok
Quoted 3 lines
(I'll review them in the meantime on-list just in the interest of moving things along.)
Quoted 5 lines
Quoted 2 lines
I was waiting for a review before updating the patches / moving to python-qemu-qmp.
Fair enough - can I kindly ask you to resend, though? My apologies and Happy New Year!
I am confused, what's the relation between QEMU python/qemu/qmp and
https://gitlab.com/qemu-project/python-qemu-qmp ?
When / how is the code sync ?
Signature · 1 line
Marc-André Lureau
Quoted 54 lines · Marc-André Lureau
Hi John
Quoted 50 lines · John Snow
Quoted 46 lines · Marc-André Lureau
Hi
Quoted 25 lines · John Snow
Quoted 23 lines · On Thu, Jun 30, 2022 at 8:34 AM <[email protected]> wrote:
From: Marc-André Lureau <[email protected]>
Hi,
As reported earlier by Richard Henderson ("virgl avocado hang" thread), avocado tests may hang when QEMU exits before the QMP connection is established.
v2:
- use asocketpair()for QMP (instead of async concurrent code from v1) as suggested by Daniel Berrange.
- should not regress (hopefully)Marc-André Lureau (3):
python/qmp/protocol: addopen_with_socket()python/qmp/legacy: make QEMUMonitorProtocol accept a socket python/qemu/machine: usesocketpair()for QMP by defaultpython/qemu/machine/machine.py | 24 ++++++++++++++++-------- python/qemu/qmp/legacy.py | 18 +++++++++++++++--- python/qemu/qmp/protocol.py | 25 ++++++++++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-)Quoted 7 lines
Quoted 3 lines
--
2.37.0.rc0For anything that touches python/qemu/qmp/*, may I please ask that you submit them to https://gitlab.com/qemu-project/python-qemu-qmp ?
Ok
Quoted 3 lines
(I'll review them in the meantime on-list just in the interest of moving things along.)
I was waiting for a review before updating the patches / moving to python-qemu-qmp.
Quoted 8 lines
Quoted 2 lines
Fair enough - can I kindly ask you to resend, though? My apologies and Happy New Year!
I am confused, what's the relation between QEMU python/qemu/qmp and
https://gitlab.com/qemu-project/python-qemu-qmp ?When / how is the code sync ?
python-qemu-qmp supersedes the code that is in qemu.git. qemu.git/python/qemu/qmp is scheduled to be deleted, but there are changes that need to go in to configure etc to support the deletion first, and I've been backlogged/waylaid on making those changes.
--js
Quoted 63 lines · John Snow
Quoted 60 lines · Marc-André Lureau
Hi John
Quoted 47 lines · John Snow
Quoted 44 lines · Marc-André Lureau
Hi
Quoted 25 lines · John Snow
Quoted 23 lines · On Thu, Jun 30, 2022 at 8:34 AM <[email protected]> wrote:
From: Marc-André Lureau <[email protected]>
Hi,
As reported earlier by Richard Henderson ("virgl avocado hang" thread), avocado tests may hang when QEMU exits before the QMP connection is established.
v2:
- use asocketpair()for QMP (instead of async concurrent code from v1) as suggested by Daniel Berrange.
- should not regress (hopefully)Marc-André Lureau (3):
python/qmp/protocol: addopen_with_socket()python/qmp/legacy: make QEMUMonitorProtocol accept a socket python/qemu/machine: usesocketpair()for QMP by defaultpython/qemu/machine/machine.py | 24 ++++++++++++++++-------- python/qemu/qmp/legacy.py | 18 +++++++++++++++--- python/qemu/qmp/protocol.py | 25 ++++++++++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-)Quoted 7 lines
Quoted 3 lines
--
2.37.0.rc0For anything that touches python/qemu/qmp/*, may I please ask that you submit them to https://gitlab.com/qemu-project/python-qemu-qmp ?
Ok
Quoted 3 lines
(I'll review them in the meantime on-list just in the interest of moving things along.)
Quoted 5 lines
Quoted 2 lines
I was waiting for a review before updating the patches / moving to python-qemu-qmp.
Fair enough - can I kindly ask you to resend, though? My apologies and Happy New Year!
I am confused, what's the relation between QEMU python/qemu/qmp and
https://gitlab.com/qemu-project/python-qemu-qmp ?
Quoted 7 lines
Quoted 2 lines
When / how is the code sync ?
python-qemu-qmp supersedes the code that is in qemu.git. qemu.git/python/qemu/qmp is scheduled to be deleted, but there are changes that need to go in to configure etc to support the deletion first, and I've been backlogged/waylaid on making those changes.
... by which I mean, I generally do review and merge on the standalone repo first, then backport to qemu.git. Or, that's what I'd prefer to do, since the tooling and testing is more advanced on the standalone repo.