diff --git a/lib/locking.py b/lib/locking.py index 5b7414dcfaecee0a0a7ee14f6e9d4d961dad47af..a0e418e990be7b951a8e1a1071283b52ae504c39 100644 --- a/lib/locking.py +++ b/lib/locking.py @@ -840,10 +840,10 @@ class SharedLock(object): if not acquired: acquired = self.__acquire_unlocked(0, timeout, priority) + if acquired: assert self.__is_exclusive() and not self.__is_sharer(), \ "Lock wasn't acquired in exclusive mode" - if acquired: self.__deleted = True self.__exc = None diff --git a/test/ganeti.locking_unittest.py b/test/ganeti.locking_unittest.py index 138e016b5f17c1885127242fd4a6b1cc6aaddeb5..c8a6807fc866a0689c4e940b17b167627f1524fa 100755 --- a/test/ganeti.locking_unittest.py +++ b/test/ganeti.locking_unittest.py @@ -433,7 +433,31 @@ class TestSharedLock(_ThreadedTestCase): self.assertRaises(errors.LockError, self.sl.delete) def testDeleteTimeout(self): - self.sl.delete(timeout=60) + self.assertTrue(self.sl.delete(timeout=60)) + + def testDeleteTimeoutFail(self): + ready = threading.Event() + finish = threading.Event() + + def fn(): + self.sl.acquire(shared=0) + ready.set() + + finish.wait() + self.sl.release() + + self._addThread(target=fn) + ready.wait() + + # Test if deleting a lock owned in exclusive mode by another thread fails + # to delete when a timeout is used + self.assertFalse(self.sl.delete(timeout=0.02)) + + finish.set() + self._waitThreads() + + self.assertTrue(self.sl.delete()) + self.assertRaises(errors.LockError, self.sl.acquire) def testNoDeleteIfSharer(self): self.sl.acquire(shared=1)