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

Appearance settings
Merged
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
[Lock] Various fixes and hardenings
  • Loading branch information
nicolas-grekas committed May 19, 2026
commit b6450a78a4085267558f2b9538d55ec019fdc882
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/Lock/Store/SemaphoreStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public static function isSupported(): bool
return \extension_loaded('sysvsem');
}

/**
* @param string $projectId A scoping prefix folded into the System V key derivation to isolate one
* application's semaphore namespace from another's running on the same host
*/
public function __construct(
private readonly string $projectId = '',
) {
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Lock/Store/StoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function createStore(#[\SensitiveParameter] object|string $connect
return new SemaphoreStore();

case str_starts_with($connection, 'semaphore://'):
return new SemaphoreStore(substr($connection, 12));
return new SemaphoreStore(rawurldecode(substr($connection, 12)));
Comment thread
nicolas-grekas marked this conversation as resolved.

case str_starts_with($connection, 'dynamodb://'):
self::requireBridgeClass(DynamoDbStore::class, 'symfony/amazon-dynamo-db-lock');
Expand Down
24 changes: 23 additions & 1 deletion 24 src/Symfony/Component/Lock/Tests/Store/SemaphoreStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Lock\Tests\Store;

use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\SemaphoreStore;
Expand Down Expand Up @@ -46,6 +47,7 @@ public function testResourceRemoval()

public function testProjectIdSeparatesLocks()
{
$initialCount = $this->getOpenedSemaphores();
$keyName = __METHOD__;
$storeA = new SemaphoreStore('project-a');
$storeB = new SemaphoreStore('project-b');
Expand All @@ -56,10 +58,30 @@ public function testProjectIdSeparatesLocks()
$storeA->save($keyA);
$storeB->save($keyB);

$this->assertSame($initialCount + 2, $this->getOpenedSemaphores(), 'Two distinct semaphores should be opened for the same key under different project IDs');

$storeA->delete($keyA);
$storeB->delete($keyB);
}

public function testSameProjectIdCollidesOnSameKey()
{
$keyName = __METHOD__;
$storeA = new SemaphoreStore('shared-project');
$storeB = new SemaphoreStore('shared-project');

$this->addToAssertionCount(1);
$keyA = new Key($keyName);
$keyB = new Key($keyName);

$storeA->save($keyA);

try {
$this->expectException(LockConflictedException::class);
$storeB->save($keyB);
} finally {
$storeA->delete($keyA);
$storeB->delete($keyB);
}
}

private function getOpenedSemaphores()
Expand Down
10 changes: 10 additions & 0 deletions 10 src/Symfony/Component/Lock/Tests/Store/StoreFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use AsyncAws\DynamoDb\DynamoDbClient;
use Doctrine\DBAL\Connection;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
Expand Down Expand Up @@ -43,6 +44,15 @@ public function testCreateStore($connection, string $expectedStoreClass)
$this->assertInstanceOf($expectedStoreClass, $store);
}

#[RequiresPhpExtension('sysvsem')]
public function testCreateSemaphoreStoreDecodesProjectId()
{
$store = StoreFactory::createStore('semaphore://my%20project%2Fid');

$this->assertInstanceOf(SemaphoreStore::class, $store);
$this->assertSame('my project/id', (new \ReflectionProperty(SemaphoreStore::class, 'projectId'))->getValue($store));
}

public static function validConnections(): \Generator
{
if (class_exists(\Redis::class)) {
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.