Skip to content

Commit

Permalink
Merge pull request #27957 from howey-aus/5.8
Browse files Browse the repository at this point in the history
[5.8] Locks acquired with block() are not immediately released if the callback fails
  • Loading branch information
taylorotwell authored Mar 21, 2019
2 parents cd7486a + 2edeb3a commit e5164f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Cache/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ public function block($seconds, $callback = null)
}

if (is_callable($callback)) {
return tap($callback(), function () {
try {
return $callback();
} finally {
$this->release();
});
}
}

return true;
Expand Down
22 changes: 22 additions & 0 deletions tests/Integration/Cache/RedisCacheLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Cache;

use Exception;
use Illuminate\Support\Carbon;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -72,6 +73,27 @@ public function test_concurrent_redis_locks_are_released_safely()
$this->assertFalse(Cache::store('redis')->lock('foo')->get());
}

public function test_redis_locks_with_failed_block_callback_are_released()
{
Cache::store('redis')->lock('foo')->forceRelease();

$firstLock = Cache::store('redis')->lock('foo', 10);

try {
$firstLock->block(1, function () {
throw new Exception('failed');
});
} catch (Exception $e) {
// Not testing the exception, just testing the lock
// is released regardless of the how the exception
// thrown by the callback was handled.
}

$secondLock = Cache::store('redis')->lock('foo', 1);

$this->assertTrue($secondLock->get());
}

public function test_redis_locks_can_be_released_using_owner_token()
{
Cache::store('redis')->lock('foo')->forceRelease();
Expand Down

0 comments on commit e5164f8

Please sign in to comment.