Skip to content

Commit 276f278

Browse files
Merge branch '3.4' into 4.0
* 3.4: [FrameworkBundle] decouple some cache-warmer's test from internal details bug #27405 [Cache] TagAwareAdapter should not corrupt memcached connection in ascii mode Remove released semaphore
2 parents e0082ab + 798b3c6 commit 276f278

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

Store/SemaphoreStore.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ private function lock(Key $key, $blocking)
6464
return;
6565
}
6666

67-
$resource = sem_get(crc32($key));
68-
$acquired = sem_acquire($resource, !$blocking);
67+
$keyId = crc32($key);
68+
$resource = sem_get($keyId);
69+
$acquired = @sem_acquire($resource, !$blocking);
70+
71+
while ($blocking && !$acquired) {
72+
$resource = sem_get($keyId);
73+
$acquired = @sem_acquire($resource);
74+
}
6975

7076
if (!$acquired) {
7177
throw new LockConflictedException();
@@ -86,7 +92,7 @@ public function delete(Key $key)
8692

8793
$resource = $key->getState(__CLASS__);
8894

89-
sem_release($resource);
95+
sem_remove($resource);
9096

9197
$key->removeState(__CLASS__);
9298
}

Tests/Store/SemaphoreStoreTest.php

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

1212
namespace Symfony\Component\Lock\Tests\Store;
1313

14+
use Symfony\Component\Lock\Key;
1415
use Symfony\Component\Lock\Store\SemaphoreStore;
1516

1617
/**
@@ -29,4 +30,31 @@ protected function getStore()
2930
{
3031
return new SemaphoreStore();
3132
}
33+
34+
public function testResourceRemoval()
35+
{
36+
$initialCount = $this->getOpenedSemaphores();
37+
$store = new SemaphoreStore();
38+
$key = new Key(uniqid(__METHOD__, true));
39+
$store->waitAndSave($key);
40+
41+
$this->assertGreaterThan($initialCount, $this->getOpenedSemaphores(), 'Semaphores should have been created');
42+
43+
$store->delete($key);
44+
$this->assertEquals($initialCount, $this->getOpenedSemaphores(), 'All semaphores should be removed');
45+
}
46+
47+
private function getOpenedSemaphores()
48+
{
49+
$lines = explode(PHP_EOL, trim(`ipcs -su`));
50+
if ('------ Semaphore Status --------' !== $lines[0]) {
51+
throw new \Exception('Failed to extract list of opend semaphores. Expect a Semaphore status, got '.implode(PHP_EOL, $lines));
52+
}
53+
list($key, $value) = explode(' = ', $lines[1]);
54+
if ('used arrays' !== $key) {
55+
throw new \Exception('Failed to extract list of opend semaphores. Expect a used arrays key, got '.implode(PHP_EOL, $lines));
56+
}
57+
58+
return (int) $value;
59+
}
3260
}

0 commit comments

Comments
 (0)