Share the cache gate with its stores and make unconditional commit last-writer-wins

An attached store takes its cache's gate as its own lock (CacheBackingStore.Gate),
so a pull's load callback into the cache can no longer take the gate after the
store lock. The directory store's private mutation gate is deleted; the
single-file pull now runs under the gate; PullAllBackingStoresAsync takes the
gate around each store's pull. Fixes the pull/upsert deadlock on directory
stores and the re-pull that erased a staged single-file write.

An unconditional single-file Commit writes the store's whole view plus the batch,
exactly as a flush would, instead of merging onto the file; it persists earlier
staged writes and leaves the store clean. Conditional commits still land onto
the file under the lock. Directory-store commit and flush already share one
generation writer (staged keys onto the current manifest) and are unchanged.

Contracts state both rules, the lock order, that OnUpdate fires for loads only,
that a missing single file keeps staged writes, and that a store failing to
hydrate on open throws and is left byte-identical.

Tests: PullAndUpsertOnDirectoryStoreDoNotDeadlock, RepullNeverErasesAStagedWrite,
UnconditionalCommitIsLastWriterWinsLikeFlush, UnconditionalCommitPersistsStagedWrites,
OpeningACorruptStoreThrowsAndLeavesItUntouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-13 18:45:27 +02:00
commit 5aad137e61
5 changed files with 236 additions and 25 deletions

View file

@ -1282,6 +1282,7 @@ namespace GameCult.Caching
}
store.AttachRegistry(_registry);
store.Gate = _gate;
store.Loaded = (loaded, dropped) => Admit(loaded, dropped, store, _ => CultCommitOutcome.Committed);
_stores.Add(candidate);
try
@ -1292,6 +1293,7 @@ namespace GameCult.Caching
{
_stores.Remove(candidate);
store.Loaded = null;
store.Gate = new object();
throw;
}
}
@ -1300,7 +1302,11 @@ namespace GameCult.Caching
public Task PullAllBackingStoresAsync()
{
foreach (var store in BackingStores)
store.PullAll();
{
lock (_gate)
store.PullAll();
}
return Task.CompletedTask;
}
@ -1800,6 +1806,10 @@ namespace GameCult.Caching
// any of it; if the cache refuses a record the call throws and the store keeps its previous view.
protected internal Action<IReadOnlyList<CultStoredDocument>, IReadOnlyList<CultStoredDocument>>? Loaded;
// Once attached, the cache's gate: the store and its cache share one lock, so a load calling back into the cache
// can never take the gate after the store lock. File locks are always taken inside it.
protected internal object Gate { get; internal set; } = new();
internal void AttachRegistry(CultDocumentRegistry registry)
{
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
@ -1881,6 +1891,12 @@ namespace GameCult.Caching
public override string ToString() => FileInfo.FullName;
public override void PullAll()
{
lock (Gate)
PullAllCore();
}
private void PullAllCore()
{
// A single-file snapshot cannot distinguish local dirty keys from clean keys. Pulling while local mutations
// are staged would let an older disk snapshot erase them before the next flush. Flush first.
@ -1959,14 +1975,15 @@ namespace GameCult.Caching
if (!request.ConditionsHold(disk.Records, Entries.Values))
return CultCommitOutcome.Mismatch;
// A clean store commits onto the file as it is now, so records this store never loaded survive. A dirty
// store's staged view is the snapshot its flush would write, so the batch lands on that.
var records = IsDirty
? Entries.Values.Select(entry => ToPersistedRecord(entry, SerializePayload)).ToDictionary(record => record.Key, StringComparer.Ordinal)
: disk.Records.ToDictionary(record => record.Key, StringComparer.Ordinal);
var catalog = IsDirty
? Entries.Values.Select(entry => entry.Descriptor.ToCatalogEntry())
: disk.SchemaCatalog;
// An unconditional commit writes what a flush would, this store's whole view plus the batch: last-writer-wins,
// and staged single writes land with it. A conditional commit (store clean) lands the batch onto the file as it is.
var ontoDisk = request.HasConditions && !IsDirty;
var records = ontoDisk
? disk.Records.ToDictionary(record => record.Key, StringComparer.Ordinal)
: Entries.Values.Select(entry => ToPersistedRecord(entry, SerializePayload)).ToDictionary(record => record.Key, StringComparer.Ordinal);
var catalog = ontoDisk
? disk.SchemaCatalog
: Entries.Values.Select(entry => entry.Descriptor.ToCatalogEntry());
foreach (var entry in request.Deletes)
records.Remove(entry.Key.Value);
foreach (var entry in request.Upserts)