From e834c54eea9eed17a171f3cd12307b821f9c41a7 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 1 Jul 2026 20:14:10 +1200 Subject: [PATCH] =?UTF-8?q?fix(cache):=20address=20review=20=E2=80=94=20ni?= =?UTF-8?q?l-guard=20window,=20self-heal=20spent=20tombstone,=20drop=20tes?= =?UTF-8?q?t=20sleep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - window: `tonumber(ARGV[n]) or 0` in all three Lua scripts, so a missing or non-numeric window fails open (treated as 0/disabled) instead of raising a Lua error that would fail the save closed. - saveWithLease now HDELs a spent/invalid/anomalous tombstone before writing, so a field-purged hash doesn't keep the tombstone forever (Redis hashes have no per-field TTL). - LeasableTest: expire the tombstone via a direct past-deadline write instead of usleep(600ms) — faster and not timing-flaky — and assert the spent field is dropped. Co-Authored-By: Claude Opus 4.8 --- src/Cache/Adapter/Redis.php | 15 +++++++++------ tests/Cache/E2E/Redis/LeasableTest.php | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Cache/Adapter/Redis.php b/src/Cache/Adapter/Redis.php index cd9673ca88..007bca7ba9 100644 --- a/src/Cache/Adapter/Redis.php +++ b/src/Cache/Adapter/Redis.php @@ -44,15 +44,17 @@ class Redis implements Adapter, Leasable, Retryable * its post-purge tombstone. redis.call('TIME') is CLOCK_REALTIME, so a * deadline more than the window ahead of now means the wall clock stepped * backward since the stamp; it is ignored rather than wedging saves, which - * also bounds any block to graceWindow of real time. A non-numeric deadline - * is ignored (fail open). Single-key. KEYS[1]=key; ARGV[1]=hash, ARGV[2]=value, - * ARGV[3]=expected generation, ARGV[4]=grace window in ms (0 disables it). + * also bounds any block to graceWindow of real time. A non-numeric window or + * deadline is treated as absent (fail open), and a spent/invalid deadline is + * dropped so a field-purged hash doesn't keep the tombstone forever. + * Single-key. KEYS[1]=key; ARGV[1]=hash, ARGV[2]=value, ARGV[3]=expected + * generation, ARGV[4]=grace window in ms (0 disables it). */ private const LUA_SAVE_WITH_LEASE = <<<'LUA' local current = redis.call('HGET', KEYS[1], '__utopia_gen__') if current == false then current = '0' end if current ~= ARGV[3] then return 0 end - local window = tonumber(ARGV[4]) + local window = tonumber(ARGV[4]) or 0 if window > 0 then local tomb = redis.call('HGET', KEYS[1], '__utopia_tomb__') if tomb ~= false then @@ -64,6 +66,7 @@ class Redis implements Adapter, Leasable, Retryable return 0 end end + redis.call('HDEL', KEYS[1], '__utopia_tomb__') end end redis.call('HSET', KEYS[1], ARGV[1], ARGV[2]) @@ -90,7 +93,7 @@ class Redis implements Adapter, Leasable, Retryable local next = (tonumber(current) or 0) + 1 redis.call('DEL', KEYS[1]) redis.call('HSET', KEYS[1], gen, next) - local window = tonumber(ARGV[1]) + local window = tonumber(ARGV[1]) or 0 if window > 0 then local t = redis.call('TIME') local now = tonumber(t[1]) * 1000000 + tonumber(t[2]) @@ -114,7 +117,7 @@ class Redis implements Adapter, Leasable, Retryable local current = redis.call('HGET', KEYS[1], '__utopia_gen__') local next = (tonumber(current) or 0) + 1 redis.call('HSET', KEYS[1], '__utopia_gen__', next) - local window = tonumber(ARGV[2]) + local window = tonumber(ARGV[2]) or 0 if window > 0 then local t = redis.call('TIME') local now = tonumber(t[1]) * 1000000 + tonumber(t[2]) diff --git a/tests/Cache/E2E/Redis/LeasableTest.php b/tests/Cache/E2E/Redis/LeasableTest.php index f38069e541..c47d6e4d72 100644 --- a/tests/Cache/E2E/Redis/LeasableTest.php +++ b/tests/Cache/E2E/Redis/LeasableTest.php @@ -168,7 +168,11 @@ class LeasableTest extends TestCase */ public function testTombstoneRejectsTokenValidSaveWithinGraceWindow(): void { - $cache = $this->graceCache(500); + $redis = new Redis(); + $redis->connect('redis', 6379); + $adapter = new RedisAdapter($redis); + $adapter->setLeaseGraceWindow(500); + $cache = new Cache($adapter); $cache->flush(); // A writer commits and purges; the tombstone opens. @@ -183,13 +187,21 @@ class LeasableTest extends TestCase ); $this->assertFalse($cache->load('doc:1', 60, 'doc:1'), 'Cache must not hold the stale value'); - // Once the grace window elapses, lease saves resume normally. - \usleep(600 * 1000); + // Simulate the window elapsing by moving the deadline into the past (a + // direct write, no sleep): lease saves resume, and the spent tombstone is + // dropped rather than lingering in the hash. + $past = ((int) \time() - 60) * 1000000; + $redis->hSet('doc:1', '__utopia_tomb__', (string) $past); + $this->assertNotFalse( $cache->saveWithLease('doc:1', ['fresh' => true], 'doc:1', $generation), 'After the grace window a token-valid save must succeed' ); $this->assertSame(['fresh' => true], $cache->load('doc:1', 60, 'doc:1')); + $this->assertFalse( + (bool) $redis->hExists('doc:1', '__utopia_tomb__'), + 'The spent tombstone must be dropped on the next save, not linger in the hash' + ); } /**