diff options
author | Julien Danjou <julien@danjou.info> | 2014-10-02 17:13:57 +0200 |
---|---|---|
committer | Julien Danjou <julien@danjou.info> | 2014-10-02 18:02:14 +0200 |
commit | aabe3c5e67cee64f55e21ed5b4d98696e0c7decd (patch) | |
tree | adffbaba9947c8bfcc33d9cc035c3983a3fccb5f | |
parent | bc664f6082979768793d87ca747965aff3cbad03 (diff) | |
download | tooz-aabe3c5e67cee64f55e21ed5b4d98696e0c7decd.tar.gz |
IPC: fix a potential race condition at init
It's possible that between the moment the code tries to get the
semaphore it has been destroyed, so let's try in a loop to catch it
until we succeed.
Change-Id: I853a7262ebd94ec954f4fb923e7547c8fd7841b4
-rw-r--r-- | tooz/drivers/ipc.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/tooz/drivers/ipc.py b/tooz/drivers/ipc.py index 905680c..5184ac7 100644 --- a/tooz/drivers/ipc.py +++ b/tooz/drivers/ipc.py @@ -45,12 +45,22 @@ class IPCLock(locking.Lock): def __init__(self, name, timeout): super(IPCLock, self).__init__(name) self.key = self.ftok(name, self._LOCK_PROJECT) - try: - self._lock = sysv_ipc.Semaphore(self.key, - flags=sysv_ipc.IPC_CREX, - initial_value=1) - except sysv_ipc.ExistentialError: - self._lock = sysv_ipc.Semaphore(self.key) + while True: + try: + # Try to create the lock + self._lock = sysv_ipc.Semaphore(self.key, + flags=sysv_ipc.IPC_CREX, + initial_value=1) + except sysv_ipc.ExistentialError: + # We failed to create it because it already exists, then try to + # grab the existing one. + try: + self._lock = sysv_ipc.Semaphore(self.key) + except sysv_ipc.ExistentialError: + # Semaphore has been deleted in the mean time, retry from + # the beginning! + pass + break self._lock.undo = True self.timeout = timeout |