[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 06/31] python/aqmp: add __del__ method to legacy interface
From: |
John Snow |
Subject: |
[PATCH v3 06/31] python/aqmp: add __del__ method to legacy interface |
Date: |
Mon, 10 Jan 2022 18:28:45 -0500 |
asyncio can complain *very* loudly if you forget to back out of things
gracefully before the garbage collector starts destroying objects that
contain live references to asyncio Tasks.
The usual fix is just to remember to call aqmp.disconnect(), but for the
sake of the legacy wrapper and quick, one-off scripts where a graceful
shutdown is not necessarily of paramount imporance, add a courtesy
cleanup that will trigger prior to seeing screenfuls of confusing
asyncio tracebacks.
Note that we can't *always* save you from yourself; depending on when
the GC runs, you might just seriously be out of luck. The best we can do
in this case is to gently remind you to clean up after yourself.
(Still much better than multiple pages of incomprehensible python
warnings for the crime of forgetting to put your toys away.)
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Beraldo Leal <bleal@redhat.com>
---
python/qemu/aqmp/legacy.py | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/python/qemu/aqmp/legacy.py b/python/qemu/aqmp/legacy.py
index 9e7b9fb80b..2ccb136b02 100644
--- a/python/qemu/aqmp/legacy.py
+++ b/python/qemu/aqmp/legacy.py
@@ -16,6 +16,8 @@
import qemu.qmp
from qemu.qmp import QMPMessage, QMPReturnValue, SocketAddrT
+from .error import AQMPError
+from .protocol import Runstate
from .qmp_client import QMPClient
@@ -136,3 +138,19 @@ def settimeout(self, timeout: Optional[float]) -> None:
def send_fd_scm(self, fd: int) -> None:
self._aqmp.send_fd_scm(fd)
+
+ def __del__(self) -> None:
+ if self._aqmp.runstate == Runstate.IDLE:
+ return
+
+ if not self._aloop.is_running():
+ self.close()
+ else:
+ # Garbage collection ran while the event loop was running.
+ # Nothing we can do about it now, but if we don't raise our
+ # own error, the user will be treated to a lot of traceback
+ # they might not understand.
+ raise AQMPError(
+ "QEMUMonitorProtocol.close()"
+ " was not called before object was garbage collected"
+ )
--
2.31.1
- [PATCH v3 00/31] Python: delete synchronous qemu.qmp package, John Snow, 2022/01/10
- [PATCH v3 01/31] python/aqmp: use absolute import statement, John Snow, 2022/01/10
- [PATCH v3 02/31] Python/aqmp: fix type definitions for mypy 0.920, John Snow, 2022/01/10
- [PATCH v3 03/31] python: update type hints for mypy 0.930, John Snow, 2022/01/10
- [PATCH v3 04/31] simplebench: Fix Python syntax error (reported by LGTM), John Snow, 2022/01/10
- [PATCH v3 05/31] python/aqmp: fix docstring typo, John Snow, 2022/01/10
- [PATCH v3 07/31] python/aqmp: handle asyncio.TimeoutError on execute(), John Snow, 2022/01/10
- [PATCH v3 06/31] python/aqmp: add __del__ method to legacy interface,
John Snow <=
- [PATCH v3 09/31] python/aqmp: add SocketAddrT to package root, John Snow, 2022/01/10
- [PATCH v3 08/31] python/aqmp: copy type definitions from qmp, John Snow, 2022/01/10
- [PATCH v3 11/31] python/qemu-ga-client: don't use deprecated CLI syntax in usage comment, John Snow, 2022/01/10
- [PATCH v3 12/31] python/qmp: switch qemu-ga-client to AQMP, John Snow, 2022/01/10
- [PATCH v3 10/31] python/aqmp: rename AQMPError to QMPError, John Snow, 2022/01/10
- [PATCH v3 16/31] python: move qmp-shell under the AQMP package, John Snow, 2022/01/10
- [PATCH v3 14/31] python/qmp: switch qmp-shell to AQMP, John Snow, 2022/01/10