From 5fb696b66a6e64b64e6d5ff85994c9fa07339804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20DESTRE?= Date: Thu, 5 Nov 2015 15:32:34 +0100 Subject: [PATCH] Release lock if the originating process is gone This fix checks if the IP of the current machine is the same that the IP of the server that acquired the lock. if we are on the same machine, but the originating process doesn't exists anymore, it assumes we can safely release the lock and return false (aka, lock is not locked). Caveat : If the lock has been acquired by a process on pc-01, but the process failed to release the lock, no other machine will be able to acquire the same lock until a new process on pc-01 will call the isLocked() method again. --- src/NinjaMutex/Lock/MemcachedLock.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/NinjaMutex/Lock/MemcachedLock.php b/src/NinjaMutex/Lock/MemcachedLock.php index 88a2051..269c770 100644 --- a/src/NinjaMutex/Lock/MemcachedLock.php +++ b/src/NinjaMutex/Lock/MemcachedLock.php @@ -115,9 +115,26 @@ public function releaseLock($name) * @param string $name name of lock * @return bool */ - public function isLocked($name) - { - return false !== $this->memcached->get($name); + public function isLocked($name) { + $value = $this->memcached->get($name); + if ($value) { + $values = unserialize($value); + // Additional verification, just in case the lock was acquired by a process that doesn't exist anymore + $informations = $this->generateLockInformation(); + $output = null; + // @todo the following command has only be tested on Debian8 & MacosX + exec("ps -p {$values[0]}", $output); + // If the original server is the same that the one we're on, but the process has gone, let's remove the LOCK from Memcache + if($informations[2] == $values[2] && (count($output) == 1)) { + $this->memcached->delete($name); + return false; + } else { + return true; + } + } else { + // Value doesn't exist, so there is no lock + return false; + } } }