From a66bd91b7074b62884839ebff79bce9e237b39f1 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 1 Oct 2009 17:22:02 +0200
Subject: [PATCH] locking.SharedLock: Fix bug in delete function
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

SharedLock.__acquire_unlocked uses keyword parameters. Just passing
the timeout would set the β€œshared” parameter.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/locking.py                  | 7 +++++--
 test/ganeti.locking_unittest.py | 3 +++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/lib/locking.py b/lib/locking.py
index a5ce5fa44..f7a62d279 100644
--- a/lib/locking.py
+++ b/lib/locking.py
@@ -520,7 +520,7 @@ class SharedLock(object):
     """
     return self.__pending[0] == cond
 
-  def __acquire_unlocked(self, shared=0, timeout=None):
+  def __acquire_unlocked(self, shared, timeout):
     """Acquire a shared lock.
 
     @param shared: whether to acquire in shared mode; by default an
@@ -641,7 +641,10 @@ class SharedLock(object):
       acquired = self.__is_exclusive()
 
       if not acquired:
-        acquired = self.__acquire_unlocked(timeout)
+        acquired = self.__acquire_unlocked(0, timeout)
+
+        assert self.__is_exclusive() and not self.__is_sharer(), \
+          "Lock wasn't acquired in exclusive mode"
 
       if acquired:
         self.__deleted = True
diff --git a/test/ganeti.locking_unittest.py b/test/ganeti.locking_unittest.py
index 31c3c606d..79c48888e 100755
--- a/test/ganeti.locking_unittest.py
+++ b/test/ganeti.locking_unittest.py
@@ -447,6 +447,9 @@ class TestSharedLock(_ThreadedTestCase):
     self.assertRaises(errors.LockError, self.sl.acquire, shared=1)
     self.assertRaises(errors.LockError, self.sl.delete)
 
+  def testDeleteTimeout(self):
+    self.sl.delete(timeout=60)
+
   def testNoDeleteIfSharer(self):
     self.sl.acquire(shared=1)
     self.assertRaises(AssertionError, self.sl.delete)
-- 
GitLab