Skip to content

[Lock] Add $prefix parameter to avoid collision with FlockStore #57857

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2009,7 +2009,7 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
$storeDefinition = new Definition(PersistingStoreInterface::class);
$storeDefinition
->setFactory([StoreFactory::class, 'createStore'])
->setArguments([$resourceStore])
->setArguments([$resourceStore, new Parameter('kernel.secret')])
->addTag('lock.store');

$container->setDefinition($storeDefinitionId = '.lock.'.$resourceName.'.store.'.$container->hash($storeDsn), $storeDefinition);
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add parameter `$prefix` to `FlockStore::__construct()`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Add parameter `$prefix` to `FlockStore::__construct()`
* Add `$prefix` parameter to `FlockStore::__construct()`


7.0
---

Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/Lock/Store/FlockStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ class FlockStore implements BlockingStoreInterface, SharedLockStoreInterface
{
private ?string $lockPath;

private ?string $prefix;

/**
* @param string|null $lockPath the directory to store the lock, defaults to the system's temporary directory
* @param string|null $prefix a prefix to add to the lock filenames to avoid collision
*
* @throws LockStorageException If the lock directory doesn’t exist or is not writable
*/
public function __construct(?string $lockPath = null)
public function __construct(?string $lockPath = null, ?string $prefix = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function __construct(?string $lockPath = null, ?string $prefix = null)
public function __construct(
?string $lockPath = null,
private ?string $prefix = null,
)

We can use CPP (constructor property promotion here)

{
if (!is_dir($lockPath ??= sys_get_temp_dir())) {
if (false === @mkdir($lockPath, 0777, true) && !is_dir($lockPath)) {
Expand All @@ -48,6 +51,7 @@ public function __construct(?string $lockPath = null)
}

$this->lockPath = $lockPath;
$this->prefix = $prefix;
}

public function save(Key $key): void
Expand Down Expand Up @@ -83,8 +87,9 @@ private function lock(Key $key, bool $read, bool $blocking): void
}

if (!$handle) {
$fileName = \sprintf('%s/sf.%s.%s.lock',
$fileName = \sprintf('%s/sf.%s%s.%s.lock',
$this->lockPath,
$this->prefix ? $this->prefix.'.' : '',
substr(preg_replace('/[^a-z0-9\._-]+/i', '-', $key), 0, 50),
strtr(substr(base64_encode(hash('sha256', $key, true)), 0, 7), '/', '_')
);
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Lock/Store/StoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
class StoreFactory
{
public static function createStore(#[\SensitiveParameter] object|string $connection): PersistingStoreInterface
public static function createStore(#[\SensitiveParameter] object|string $connection, #[\SensitiveParameter] ?string $secret = null): PersistingStoreInterface
{
switch (true) {
case $connection instanceof \Redis:
Expand Down Expand Up @@ -54,6 +54,9 @@ public static function createStore(#[\SensitiveParameter] object|string $connect
case 'flock' === $connection:
return new FlockStore();

case str_starts_with($connection, 'flock+exclusive://'):
return new FlockStore(substr($connection, 15), $secret);

case str_starts_with($connection, 'flock://'):
return new FlockStore(substr($connection, 8));

Expand Down
25 changes: 23 additions & 2 deletions src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class FlockStoreTest extends AbstractStoreTestCase
use SharedLockStoreTestTrait;
use UnserializableTestTrait;

protected function getStore(): PersistingStoreInterface
protected function getStore(?string $prefix = null): PersistingStoreInterface
{
return new FlockStore();
return new FlockStore(null, $prefix);
}

public function testConstructWhenRepositoryCannotBeCreated()
Expand Down Expand Up @@ -101,4 +101,25 @@ public function testSaveSanitizeLongName()

$store->delete($key);
}

public function testSavePrefix()
{
$store = $this->getStore('FlockSecret');

$key = new Key(__CLASS__);

$file = \sprintf(
'%s/sf.FlockSecret.Symfony-Component-Lock-Tests-Store-FlockStoreTest.%s.lock',
sys_get_temp_dir(),
strtr(substr(base64_encode(hash('sha256', $key, true)), 0, 7), '/', '_')
);
// ensure the file does not exist before the store
@unlink($file);

$store->save($key);

$this->assertFileExists($file);

$store->delete($key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ public static function validConnections(): \Generator

yield ['flock', FlockStore::class];
yield ['flock://'.sys_get_temp_dir(), FlockStore::class];
yield ['flock+exclusive://'.sys_get_temp_dir(), FlockStore::class];
}
}