lockedobjects_db: Extract nested lock detection to a method

Partial-Bug: #1740667
Change-Id: If349f337ae0279b0b846ec92e0df209700bc83c5
This commit is contained in:
Omer Anson 2018-04-11 12:29:39 +03:00
parent 3945b4eb7f
commit d083aa6211
1 changed files with 8 additions and 6 deletions

View File

@ -63,6 +63,13 @@ class wrap_db_lock(object):
super(wrap_db_lock, self).__init__()
self.type = type
def is_within_wrapper(self):
# magic to prevent from nested lock
for frame in inspect.stack()[1:]:
if frame[3] == 'wrap_db_lock':
return True
return False
def __call__(self, f):
@six.wraps(f)
def wrap_db_lock(*args, **kwargs):
@ -70,12 +77,7 @@ class wrap_db_lock(object):
result = None
lock_id = _get_lock_id_by_resource_type(self.type, *args, **kwargs)
# magic to prevent from nested lock
within_wrapper = False
for frame in inspect.stack()[1:]:
if frame[3] == 'wrap_db_lock':
within_wrapper = True
break
within_wrapper = self.is_within_wrapper()
if not within_wrapper:
# test and create the lock if necessary