fix(cache): address review — nil-guard window, self-heal spent tombstone, drop test sleep

- 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 <noreply@anthropic.com>
This commit is contained in:
Jake BarnbyandClaude Opus 4.8 committed 2026-07-01 20:14:10 +12:00
1 parent 85eab20670
commit e834c54eea
2 files changed
+24 -9

No files matched your search

+9 -6
View File
@@ -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])
+15 -3
View File
@@ -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'
);
}
/**