Skip to content

Commit 9cb1f75

Browse files
committed
Clear stale errors before evalSha in evalWithShaCache
Add clearLastError() call before evalSha() to prevent stale errors from previous commands causing incorrect error detection. Without this, a successful evalSha returning nil could be mistaken for a NOSCRIPT error if a previous command left an error in the buffer. Add unit test verifying the call order.
1 parent 21bf5a0 commit 9cb1f75

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/redis/src/RedisConnection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,10 @@ public function evalWithShaCache(string $script, array $keys = [], array $args =
829829
// combined_args = keys first, then other args
830830
$combinedArgs = [...$keys, ...$args];
831831

832+
// Clear any stale error from previous commands to ensure getLastError()
833+
// reflects this call, not a previous one
834+
$this->connection->clearLastError();
835+
832836
// Try evalSha first - uses cached compiled script
833837
$result = $this->connection->evalSha($sha, $combinedArgs, $numKeys);
834838

tests/Redis/RedisConnectionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,32 @@ public function testEvalWithShaCacheWorksWithMultipleKeysAndArgs(): void
832832
$this->assertEquals(['key1', 'key2', 'arg1', 'arg2'], $result);
833833
}
834834

835+
public function testEvalWithShaCacheClearsLastErrorBeforeEvalSha(): void
836+
{
837+
$connection = $this->mockRedisConnection();
838+
$script = 'return "ok"';
839+
$sha = sha1($script);
840+
841+
$redisConnection = $connection->getConnection();
842+
843+
// Verify clearLastError is called before evalSha using ordered expectations
844+
$redisConnection->shouldReceive('clearLastError')
845+
->once()
846+
->globally()
847+
->ordered();
848+
849+
$redisConnection->shouldReceive('evalSha')
850+
->with($sha, [], 0)
851+
->once()
852+
->globally()
853+
->ordered()
854+
->andReturn('ok');
855+
856+
$result = $connection->evalWithShaCache($script);
857+
858+
$this->assertEquals('ok', $result);
859+
}
860+
835861
protected function mockRedisConnection(?ContainerInterface $container = null, ?PoolInterface $pool = null, array $options = [], bool $transform = false): RedisConnection
836862
{
837863
$connection = new RedisConnectionStub(

0 commit comments

Comments
 (0)