Change in vdsm[ovirt-3.6]: utils: add weakmethod helper
fromani at redhat.com
fromani at redhat.com
Tue Jan 19 12:35:31 UTC 2016
Hello Nir Soffer, Martin Polednik,
I'd like you to do a code review. Please visit
https://gerrit.ovirt.org/52422
to review the following change.
Change subject: utils: add weakmethod helper
......................................................................
utils: add weakmethod helper
The libvirtconnection module adds wrapping around virDomain
objects, to detect and react libvirt issues, like timeouts and
connection errors.
This patch adds a helper function to wrap instance methods
avoiding circular references which in turn may render some objects
uncollectable, causing leaks.
Note: this patch previously included tests, which were
stripped to another patch, because of unrelated failures
made them failed constantly.
Change-Id: I9f26aa314e26142122e9f594275406cf7fbade98
Bug-Url: https://bugzilla.redhat.com/1283999
Backport-To: 3.6
Backport-To: 3.5
Reported-By: Eldad Marciano <emarcian at redhat.com>
Reported-By: Milan Zamazal <mzamazal at redhat.com>
Signed-off-by: Francesco Romani <fromani at redhat.com>
Reviewed-on: https://gerrit.ovirt.org/51865
Reviewed-by: Nir Soffer <nsoffer at redhat.com>
Reviewed-by: Martin Polednik <mpolednik at redhat.com>
Continuous-Integration: Jenkins CI
---
M lib/vdsm/utils.py
1 file changed, 28 insertions(+), 0 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/22/52422/1
diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py
index 457ece0..c67581b 100644
--- a/lib/vdsm/utils.py
+++ b/lib/vdsm/utils.py
@@ -53,6 +53,8 @@
import string
import threading
import time
+import weakref
+
import vdsm.infra.zombiereaper as zombiereaper
from cpopen import CPopen
@@ -1285,3 +1287,29 @@
original order.
"""
return OrderedDict.fromkeys(iterable).keys()
+
+
+class InvalidatedWeakRef(Exception):
+ """
+ Stale weakref, the object was deallocated
+ """
+
+
+def weakmethod(meth):
+ """
+ Return a weakly-referenced wrapper for an instance method.
+ Use this function when you want to decorate an instance method
+ from the outside, to avoid reference cycles.
+ Raise InvalidatedWeakRef if the related instance was collected,
+ so the wrapped method is no longer usable.
+ """
+ func = meth.__func__
+ ref = weakref.ref(meth.__self__)
+
+ def wrapper(*args, **kwargs):
+ inst = ref()
+ if inst is None:
+ raise InvalidatedWeakRef()
+ return func(inst, *args, **kwargs)
+
+ return wrapper
--
To view, visit https://gerrit.ovirt.org/52422
To unsubscribe, visit https://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I9f26aa314e26142122e9f594275406cf7fbade98
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: ovirt-3.6
Gerrit-Owner: Francesco Romani <fromani at redhat.com>
Gerrit-Reviewer: Martin Polednik <mpolednik at redhat.com>
Gerrit-Reviewer: Nir Soffer <nsoffer at redhat.com>
More information about the vdsm-patches
mailing list