Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9f960f3

Browse files
committed
Fix expired lock not cleaned
1 parenta8520ae commit9f960f3

File tree

6 files changed

+77
-18
lines changed

6 files changed

+77
-18
lines changed

‎src/Symfony/Component/Lock/Lock.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public function acquire($blocking = false)
8383
}
8484

8585
if ($this->key->isExpired()) {
86+
try {
87+
$this->release();
88+
}catch (\Exception$e) {
89+
// swallow exception to not hide the original issue
90+
}
8691
thrownewLockExpiredException(sprintf('Failed to store the "%s" lock.',$this->key));
8792
}
8893

@@ -117,6 +122,11 @@ public function refresh()
117122
$this->dirty =true;
118123

119124
if ($this->key->isExpired()) {
125+
try {
126+
$this->release();
127+
}catch (\Exception$e) {
128+
// swallow exception to not hide the original issue
129+
}
120130
thrownewLockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.',$this->key));
121131
}
122132

‎src/Symfony/Component/Lock/Store/CombinedStore.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
usePsr\Log\NullLogger;
1717
useSymfony\Component\Lock\Exception\InvalidArgumentException;
1818
useSymfony\Component\Lock\Exception\LockConflictedException;
19-
useSymfony\Component\Lock\Exception\LockExpiredException;
2019
useSymfony\Component\Lock\Exception\NotSupportedException;
2120
useSymfony\Component\Lock\Key;
2221
useSymfony\Component\Lock\StoreInterface;
@@ -30,6 +29,7 @@
3029
class CombinedStoreimplements StoreInterface, LoggerAwareInterface
3130
{
3231
use LoggerAwareTrait;
32+
use ExpiringStoreTrait;
3333

3434
/** @var StoreInterface[] */
3535
private$stores;
@@ -78,6 +78,8 @@ public function save(Key $key)
7878
}
7979
}
8080

81+
$this->checkNotExpired($key);
82+
8183
if ($this->strategy->isMet($successCount,$storesCount)) {
8284
return;
8385
}
@@ -125,9 +127,7 @@ public function putOffExpiration(Key $key, $ttl)
125127
}
126128
}
127129

128-
if ($key->isExpired()) {
129-
thrownewLockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.',$key));
130-
}
130+
$this->checkNotExpired($key);
131131

132132
if ($this->strategy->isMet($successCount,$storesCount)) {
133133
return;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Lock\Store;
13+
14+
useSymfony\Component\Lock\Exception\LockExpiredException;
15+
useSymfony\Component\Lock\Key;
16+
17+
trait ExpiringStoreTrait
18+
{
19+
privatefunctioncheckNotExpired(Key$key)
20+
{
21+
if ($key->isExpired()) {
22+
try {
23+
$this->delete($key);
24+
}catch (\Exception$e) {
25+
// swallow exception to not hide the original issue
26+
}
27+
thrownewLockExpiredException(sprintf('Failed to store the "%s" lock.',$key));
28+
}
29+
}
30+
}

‎src/Symfony/Component/Lock/Store/MemcachedStore.php‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
useSymfony\Component\Lock\Exception\InvalidArgumentException;
1515
useSymfony\Component\Lock\Exception\LockConflictedException;
16-
useSymfony\Component\Lock\Exception\LockExpiredException;
1716
useSymfony\Component\Lock\Key;
1817
useSymfony\Component\Lock\StoreInterface;
1918

@@ -24,6 +23,8 @@
2423
*/
2524
class MemcachedStoreimplements StoreInterface
2625
{
26+
use ExpiringStoreTrait;
27+
2728
private$memcached;
2829
private$initialTtl;
2930
/** @var bool */
@@ -64,9 +65,7 @@ public function save(Key $key)
6465
$this->putOffExpiration($key,$this->initialTtl);
6566
}
6667

67-
if ($key->isExpired()) {
68-
thrownewLockExpiredException(sprintf('Failed to store the "%s" lock.',$key));
69-
}
68+
$this->checkNotExpired($key);
7069
}
7170

7271
publicfunctionwaitAndSave(Key$key)
@@ -110,9 +109,7 @@ public function putOffExpiration(Key $key, $ttl)
110109
thrownewLockConflictedException();
111110
}
112111

113-
if ($key->isExpired()) {
114-
thrownewLockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.',$key));
115-
}
112+
$this->checkNotExpired($key);
116113
}
117114

118115
/**

‎src/Symfony/Component/Lock/Store/RedisStore.php‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
useSymfony\Component\Cache\Traits\RedisProxy;
1515
useSymfony\Component\Lock\Exception\InvalidArgumentException;
1616
useSymfony\Component\Lock\Exception\LockConflictedException;
17-
useSymfony\Component\Lock\Exception\LockExpiredException;
1817
useSymfony\Component\Lock\Key;
1918
useSymfony\Component\Lock\StoreInterface;
2019

@@ -25,6 +24,8 @@
2524
*/
2625
class RedisStoreimplements StoreInterface
2726
{
27+
use ExpiringStoreTrait;
28+
2829
private$redis;
2930
private$initialTtl;
3031

@@ -66,9 +67,7 @@ public function save(Key $key)
6667
thrownewLockConflictedException();
6768
}
6869

69-
if ($key->isExpired()) {
70-
thrownewLockExpiredException(sprintf('Failed to store the "%s" lock.',$key));
71-
}
70+
$this->checkNotExpired($key);
7271
}
7372

7473
publicfunctionwaitAndSave(Key$key)
@@ -94,9 +93,7 @@ public function putOffExpiration(Key $key, $ttl)
9493
thrownewLockConflictedException();
9594
}
9695

97-
if ($key->isExpired()) {
98-
thrownewLockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.',$key));
99-
}
96+
$this->checkNotExpired($key);
10097
}
10198

10299
/**

‎src/Symfony/Component/Lock/Tests/Store/ExpiringStoreTestTrait.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Component\Lock\Tests\Store;
1313

14+
useSymfony\Component\Lock\Exception\LockExpiredException;
1415
useSymfony\Component\Lock\Key;
1516
useSymfony\Component\Lock\StoreInterface;
1617

@@ -105,4 +106,28 @@ public function testSetExpiration()
105106
$this->assertGreaterThanOrEqual(0,$key->getRemainingLifetime());
106107
$this->assertLessThanOrEqual(1,$key->getRemainingLifetime());
107108
}
109+
110+
publicfunctiontestExpiredLockCleaned()
111+
{
112+
$resource =uniqid(__METHOD__,true);
113+
114+
$key1 =newKey($resource);
115+
$key2 =newKey($resource);
116+
117+
/** @var StoreInterface $store */
118+
$store =$this->getStore();
119+
$key1->reduceLifetime(0);
120+
121+
$this->assertTrue($key1->isExpired());
122+
try {
123+
$store->save($key1);
124+
$this->fail('The store shouldn\'t have save an expired key');
125+
}catch (LockExpiredException$e) {
126+
}
127+
128+
$this->assertFalse($store->exists($key1));
129+
130+
$store->save($key2);
131+
$this->assertTrue($store->exists($key2));
132+
}
108133
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp