Skip to content

[Lock] Fix predis command error checking #59348

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
Jan 4, 2025
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
39 changes: 17 additions & 22 deletions src/Symfony/Component/Lock/Store/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Lock\Store;

use Predis\Response\ServerException;
use Predis\Response\Error;
use Relay\Relay;
use Symfony\Component\Lock\Exception\InvalidTtlException;
use Symfony\Component\Lock\Exception\LockConflictedException;
Expand Down Expand Up @@ -250,10 +250,10 @@ private function evaluate(string $script, string $resource, array $args): mixed
}

$result = $this->redis->evalSha($scriptSha, array_merge([$resource], $args), 1);
}

if (null !== $err = $this->redis->getLastError()) {
throw new LockStorageException($err);
}
if (null !== $err = $this->redis->getLastError()) {
throw new LockStorageException($err);
}

return $result;
Expand All @@ -273,37 +273,32 @@ private function evaluate(string $script, string $resource, array $args): mixed
}

$result = $client->evalSha($scriptSha, array_merge([$resource], $args), 1);
}

if (null !== $err = $client->getLastError()) {
throw new LockStorageException($err);
}
if (null !== $err = $client->getLastError()) {
throw new LockStorageException($err);
}

return $result;
}

\assert($this->redis instanceof \Predis\ClientInterface);

try {
return $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
} catch (ServerException $e) {
// Fallthrough only if we need to load the script
if (self::NO_SCRIPT_ERROR_MESSAGE !== $e->getMessage()) {
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
$result = $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
if ($result instanceof Error && self::NO_SCRIPT_ERROR_MESSAGE === $result->getMessage()) {
$result = $this->redis->script('LOAD', $script);
if ($result instanceof Error) {
throw new LockStorageException($result->getMessage());
}
}

try {
$this->redis->script('LOAD', $script);
} catch (ServerException $e) {
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
$result = $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
}

try {
return $this->redis->evalSha($scriptSha, 1, $resource, ...$args);
} catch (ServerException $e) {
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
if ($result instanceof Error) {
throw new LockStorageException($result->getMessage());
}

return $result;
}

private function getUniqueToken(Key $key): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ protected function getClockDelay(): int

public function getStore(): PersistingStoreInterface
{
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]));
$redis = new \Predis\Client(
array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => 6379]),
['exceptions' => false],
);

try {
$redis->connect();
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/Lock/Tests/Store/PredisStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class PredisStoreTest extends AbstractRedisStoreTestCase
{
public static function setUpBeforeClass(): void
{
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]));
$redis = new \Predis\Client(
array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]),
['exceptions' => false],
);
try {
$redis->connect();
} catch (\Exception $e) {
Expand All @@ -30,7 +33,10 @@ public static function setUpBeforeClass(): void

protected function getRedisConnection(): \Predis\Client
{
$redis = new \Predis\Client(array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]));
$redis = new \Predis\Client(
array_combine(['host', 'port'], explode(':', getenv('REDIS_HOST')) + [1 => null]),
['exceptions' => false],
);
$redis->connect();

return $redis;
Expand Down