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

[Lock] Release DoctrineDbalPostgreSqlStore connection lock on failure #44828

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

Merged
merged 1 commit into from
Dec 29, 2021
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] Release DoctrineDbalPostgreSqlStore connection lock on failure
  • Loading branch information
simon-watiau authored and nicolas-grekas committed Dec 29, 2021
commit fab59916869c7ab33e6a2b71f299b403f4f431d8
60 changes: 40 additions & 20 deletions 60 src/Symfony/Component/Lock/Store/DoctrineDbalPostgreSqlStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,28 @@ public function save(Key $key)
// prevent concurrency within the same connection
$this->getInternalStore()->save($key);

$sql = 'SELECT pg_try_advisory_lock(:key)';
$result = $this->conn->executeQuery($sql, [
'key' => $this->getHashedKey($key),
]);
$lockAcquired = false;

// Check if lock is acquired
if (true === $result->fetchOne()) {
$key->markUnserializable();
// release sharedLock in case of promotion
$this->unlockShared($key);
try {
$sql = 'SELECT pg_try_advisory_lock(:key)';
$result = $this->conn->executeQuery($sql, [
'key' => $this->getHashedKey($key),
]);

return;
// Check if lock is acquired
if (true === $result->fetchOne()) {
$key->markUnserializable();
// release sharedLock in case of promotion
$this->unlockShared($key);

$lockAcquired = true;

return;
}
} finally {
if (!$lockAcquired) {
$this->getInternalStore()->delete($key);
}
}

throw new LockConflictedException();
Expand All @@ -84,18 +94,28 @@ public function saveRead(Key $key)
// prevent concurrency within the same connection
$this->getInternalStore()->saveRead($key);

$sql = 'SELECT pg_try_advisory_lock_shared(:key)';
$result = $this->conn->executeQuery($sql, [
'key' => $this->getHashedKey($key),
]);
$lockAcquired = false;

try {
$sql = 'SELECT pg_try_advisory_lock_shared(:key)';
$result = $this->conn->executeQuery($sql, [
'key' => $this->getHashedKey($key),
]);

// Check if lock is acquired
if (true === $result->fetchOne()) {
$key->markUnserializable();
// release lock in case of demotion
$this->unlock($key);
// Check if lock is acquired
if (true === $result->fetchOne()) {
$key->markUnserializable();
// release lock in case of demotion
$this->unlock($key);

return;
$lockAcquired = true;

return;
}
} finally {
if (!$lockAcquired) {
$this->getInternalStore()->delete($key);
}
}

throw new LockConflictedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\DriverManager;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\DoctrineDbalPostgreSqlStore;
Expand Down Expand Up @@ -59,4 +60,30 @@ public function getInvalidDrivers()
yield ['sqlite:///tmp/foo.db'];
yield [DriverManager::getConnection(['url' => 'sqlite:///tmp/foo.db'])];
}

public function testSaveAfterConflict()
{
$store1 = $this->getStore();
$store2 = $this->getStore();

$key = new Key(uniqid(__METHOD__, true));

$store1->save($key);
$this->assertTrue($store1->exists($key));

$lockConflicted = false;
try {
$store2->save($key);
} catch (LockConflictedException $lockConflictedException) {
$lockConflicted = true;
}

$this->assertTrue($lockConflicted);
$this->assertFalse($store2->exists($key));

$store1->delete($key);

$store2->save($key);
$this->assertTrue($store2->exists($key));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.