From f322f8d0a2942be5d62ec45b27bd390d0f5f696d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Mon, 7 Oct 2024 03:12:21 +0200 Subject: [PATCH] VirDomainWrapper: avoid keeping libvirt object ref in locals Complementary to the previous commit, avoid keeping keeping libvirt object (function) reference in locals, which become closure for the wrapper. Do getattr (again) when the actual call is made. The primary reason for this change is to avoid leaking objects during tests, but also this fixes a rare failure more - if external caller would cache returned function, it wouldn't work after reconnection. For example: # libvirt_domain is VirDomainWrapper() object >>> func = libvirt_domain.pause >>> func() >>> func() If reconnection happens during the first call (or between calls), the second call would use stale function reference. --- qubes/app.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/qubes/app.py b/qubes/app.py index 02bc84d61..4031f8386 100644 --- a/qubes/app.py +++ b/qubes/app.py @@ -104,12 +104,14 @@ def __getattr__(self, attrname): @functools.wraps(attr) def wrapper(*args, **kwargs): try: - return attr(*args, **kwargs) + return getattr(self._vm, attrname)(*args, **kwargs) except libvirt.libvirtError: if self._reconnect_if_dead(): return getattr(self._vm, attrname)(*args, **kwargs) raise + del wrapper.__wrapped__ + del attr return wrapper