pyocf: Improve volume instance lifecycle handling

We need a strong reference to volume for as long as OCF has it open.
For that I changed instance tracking dictionary from weakvalue to
normal. This way I made sure that GC won't clean up Volume before its
closed.

Signed-off-by: Jan Musial <jan.musial@intel.com>
This commit is contained in:
Jan Musial 2022-06-03 10:36:12 +02:00
parent 83a28825d2
commit 1bdf4a13ae

View File

@ -89,7 +89,7 @@ VOLUME_POISON = 0x13
class Volume: class Volume:
_instances_ = weakref.WeakValueDictionary() _instances_ = {}
_uuid_ = weakref.WeakValueDictionary() _uuid_ = weakref.WeakValueDictionary()
_ops_ = {} _ops_ = {}
_props_ = {} _props_ = {}
@ -183,8 +183,8 @@ class Volume:
if volume.opened: if volume.opened:
return -OcfErrorCode.OCF_ERR_NOT_OPEN_EXC return -OcfErrorCode.OCF_ERR_NOT_OPEN_EXC
Volume._instances_[ref] = volume
volume.handle = ref volume.handle = ref
Volume._instances_[ref] = volume
ret = volume.do_open() ret = volume.do_open()
if ret == 0: if ret == 0:
@ -228,11 +228,11 @@ class Volume:
@classmethod @classmethod
def get_instance(cls, ref): def get_instance(cls, ref):
instance = cls._instances_[ref] if ref not in cls._instances_:
if instance is None:
print("tried to access {} but it's gone".format(ref)) print("tried to access {} but it's gone".format(ref))
return None
return instance return cls._instances_[ref]
@classmethod @classmethod
def get_by_uuid(cls, uuid): def get_by_uuid(cls, uuid):
@ -273,7 +273,7 @@ class Volume:
return 0 return 0
def do_close(self): def do_close(self):
pass del Volume._instances_[self.handle]
def get_length(self): def get_length(self):
raise NotImplementedError raise NotImplementedError