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 1d066fd
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion qubes/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ def __getattr__(self, attrname):
@functools.wraps(attr)
def wrapper(*args, **kwargs):
try:
return attr(*args, **kwargs)
return getattr(self._vm, attrname)(*args, **kwargs)

Check warning on line 107 in qubes/app.py

View check run for this annotation

Codecov / codecov/patch

qubes/app.py#L107

Added line #L107 was not covered by tests
except libvirt.libvirtError:
if self._reconnect_if_dead():
return getattr(self._vm, attrname)(*args, **kwargs)
raise

del attr

Check warning on line 113 in qubes/app.py

View check run for this annotation

Codecov / codecov/patch

qubes/app.py#L113

Added line #L113 was not covered by tests
return wrapper


Expand Down

0 comments on commit 1d066fd

Please sign in to comment.