Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop in mutex dtor when backend is unavailable #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/NinjaMutex/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace NinjaMutex;

use NinjaMutex\Lock\LockInterface;
use NinjaMutex\MutexException;

/**
* Mutex
Expand All @@ -18,13 +19,6 @@
*/
class Mutex
{
/**
* Is mutex acquired?
*
* @var bool
*/
protected $acquired = false;

/**
* Lock implementor
*
Expand Down Expand Up @@ -62,9 +56,10 @@ public function __construct($name, LockInterface $lockImplementor)
*/
public function acquireLock($timeout = null)
{
if ($this->counter > 0 || $this->lockImplementor->acquireLock($this->name, $timeout)) {
if ($this->counter > 0 ||
$this->lockImplementor->acquireLock($this->name, $timeout)) {
$this->counter++;
return $this->acquired = true;
return true;
}

return false;
Expand All @@ -75,23 +70,27 @@ public function acquireLock($timeout = null)
*/
public function releaseLock()
{
if ($this->acquired) {
if ($this->counter > 0) {
$this->counter--;
if ($this->counter > 0) {
if ($this->counter > 0 ||
$this->lockImplementor->releaseLock($this->name)) {
return true;
}

return !($this->acquired = !$this->lockImplementor->releaseLock($this->name));
$this->counter++;
}

return false;
}

public function __destruct()
{
// If we acquired lock then we should release it
while ($this->acquired) {
$this->releaseLock();
while ($this->counter > 0) {
if (!$this->releaseLock()) {
throw new MutexException(sprintf(
'Cannot release lock: %s',
$this->name
));
}
}
}

Expand All @@ -102,7 +101,7 @@ public function __destruct()
*/
public function isAcquired()
{
return $this->acquired;
return $this->counter > 0;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/NinjaMutex/Mock/MockPredisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class MockPredisClient extends Predis\Client
*/
protected static $data = array();

/**
* Whether the service is available
* @var boolean
*/
private $available = true;

public function __construct()
{
}
Expand All @@ -34,6 +40,9 @@ public function __construct()
*/
public function setnx($key, $value)
{
if (!$this->available) {
return false;
}
if (null === $this->get($key)) {
self::$data[$key] = (string)$value;
return true;
Expand All @@ -48,6 +57,9 @@ public function setnx($key, $value)
*/
public function get($key)
{
if (!$this->available) {
return false;
}
if (!isset(self::$data[$key])) {
return null;
}
Expand All @@ -61,7 +73,18 @@ public function get($key)
*/
public function del($key)
{
if (!$this->available) {
return false;
}
unset(self::$data[$key]);
return true;
}

/**
* @param boolean $b
*/
public function setAvailable($b = true)
{
$this->available = (bool) $b;
}
}
23 changes: 23 additions & 0 deletions tests/NinjaMutex/MutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use NinjaMutex\AbstractTest;
use NinjaMutex\Lock\LockAbstract;
use NinjaMutex\Lock\LockInterface;
use NinjaMutex\Mock\MockPredisClient;
use NinjaMutex\Lock\PredisRedisLock;
use NinjaMutex\MutexException;
use NinjaMutex\Mutex;

/**
Expand Down Expand Up @@ -188,4 +191,24 @@ public function testIfMutexIsReusableAfterSeveralAcquireReleaseCycles(LockInterf
// cleanup
$secondMutex->releaseLock();
}

/**
* @expectedException NinjaMutex\MutexException
*/
public function testIfMutexDestructorThrowsWhenBackendIsUnavailable() {
$predisMock = new MockPredisClient();
$lockImplementor = new PredisRedisLock($predisMock);
$mutex = new Mutex('forfiter', $lockImplementor);

$this->assertFalse($mutex->isAcquired());
$mutex->acquireLock();
$this->assertTrue($mutex->isAcquired());
$mutex->acquireLock();
$this->assertTrue($mutex->isAcquired());

// make backend unavailable
$predisMock->setAvailable(false);
// explicit dtor call, should throw MutexException
$mutex->__destruct();
}
}