Refuse a key in two stores, stop reporting reader contention, prove lock waits and page order

F2: Validate refuses a write or load that would give a key already held by
one home store a second store; the message names the key and both stores and
nothing lands in either store or the cache.
F3: TryCommit enters the cache gate normally; only the store lock attempt is
non-blocking, so Contended means another writer holds the store.
F4: PushAll and Commit are shown to wait on a held <path>.lock; the race test
asserts mismatches occurred and that Task.WaitAll finished in time.
F5: a failed page write leaves the manifest bytes and the previous generation
intact.

Lock order (F1) and commit merge semantics (F6) are unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-13 18:21:19 +02:00
commit 2a44209510
4 changed files with 185 additions and 11 deletions

View file

@ -1505,9 +1505,8 @@ namespace GameCult.Caching
batch.Sealed = true;
}
if (!Monitor.TryEnter(_gate, wait ? Timeout.Infinite : 0))
return CultCommitOutcome.Contended;
try
// The in-process gate is always entered; only the store lock attempt honors wait, so Contended means another writer holds the store.
lock (_gate)
{
var upserts = batch.Operations.Values.Where(op => op.Stored != null).Select(op => op.Stored!).ToArray();
var deletes = batch.Operations.Values
@ -1532,10 +1531,6 @@ namespace GameCult.Caching
return request.ConditionsHold(inMemory, _entries.Values) ? CultCommitOutcome.Committed : CultCommitOutcome.Mismatch;
});
}
finally
{
Monitor.Exit(_gate);
}
}
// Every add and remove passes here: single writes, committed batches, and loads (source set).
@ -1590,6 +1585,15 @@ namespace GameCult.Caching
throw new InvalidOperationException(
$"{descriptor.SchemaName} record {stored.Key.Value} was loaded from {source} but its home is {home?.ToString() ?? "no store"}.");
// One key, one store: a key never moves between homes, and two stores never both deliver it.
if (_entries.TryGetValue(stored.Key.Value, out var present) &&
Home(present.Descriptor.DocumentType) is var held &&
Home(descriptor.DocumentType) is var incoming &&
held != incoming)
throw new InvalidOperationException(
$"Record {stored.Key.Value} is held by {held?.ToString() ?? "memory"}; " +
$"{(source == null ? "writing" : "loading")} it as {descriptor.SchemaName} from {incoming?.ToString() ?? "memory"} would give the key a second store.");
if (!descriptor.IsGlobal)
continue;
if (globals.TryGetValue(descriptor.DocumentType, out var existing) && existing != stored.Key.Value)