Skip to content

Commit

Permalink
VirDomainWrapper: avoid keeping libvirt object ref in locals
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marmarek committed Oct 7, 2024
1 parent abbcf6e commit f322f8d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion qubes/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit f322f8d

Please sign in to comment.