Route every admitting gate entry through a hold

FlushAllBackingStores and Dispose take the gate through Held, so a store
method that loads can never reach Admit under a plain lock; Admit outside
a hold throws a clear InvalidOperationException. Entering a cache's hold
inside another cache's hold throws. PullAllBackingStoresAsync pulls every
store even when an OnUpdate handler throws, then rethrows. The dead Try
around Subject.OnNext is gone: R3 never throws there, so only OnUpdate
exceptions reach the caller. Repull tests wait for the writer to start
before their bounded wait.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-13 20:01:36 +02:00
commit b653f68f81
4 changed files with 124 additions and 28 deletions

View file

@ -49,11 +49,14 @@ which route wrote it.
published after the cache releases its gate and before the call that made it
returns, on that caller's thread; no call publishes another call's changes. A
direct `PullAll` on an attached store is such a call.
- An observer exception is rethrown to that caller after all of that call's
changes are delivered (an `AggregateException` if several threw). If the call
itself failed, its own exception is rethrown and observer exceptions are
dropped. A throwing observer cannot undo the store's adoption of a load; the
store and cache already agree when publication starts.
- An `OnUpdate` handler exception is rethrown to that caller after all of that
call's changes are delivered (an `AggregateException` if several threw). If
the call itself failed, its own exception is rethrown and handler exceptions
are dropped. A `Watch` subscriber exception is not rethrown; it follows R3's
unhandled-exception handling. A throwing handler cannot undo the store's
adoption of a load; the store and cache already agree when publication starts.
- Pulling all stores pulls every attached store even if a handler throws during
one store's load, then rethrows.
- Hydration failure on open is loud: a corrupt store file, or a record whose
schema the registry cannot resolve, makes the open throw and leaves the file
byte-identical. Consumers never delete and rewrite a store they failed to
@ -63,7 +66,10 @@ which route wrote it.
- One lock order: the cache's gate, then the store's lock. A store attached to a
cache takes that cache's gate as its own lock, so a store's load callback into
the cache cannot take the two out of order.
the cache cannot take the two out of order. A store must not call `Loaded`
while holding its own lock outside the cache's hold.
- A thread holds one cache's gate at a time: entering a cache's hold inside
another cache's hold throws.
- A store doing I/O on behalf of its cache (pull, flush, commit) holds the gate,
so that cache's readers wait for the I/O. The file lock (`<path>.lock` or the
directory commit lease) is always taken inside the gate and released before a

View file

@ -1306,22 +1306,36 @@ namespace GameCult.Caching
public Task PullAllBackingStoresAsync()
{
// Our stores take the gate themselves; a third-party store might not, so the cache takes it here.
// One store's throwing OnUpdate handler does not stop the others from loading.
var failures = new List<Exception>();
foreach (var store in BackingStores)
Held(() => { store.PullAll(); return true; });
{
try
{
Held(() => { store.PullAll(); return true; });
}
catch (Exception exception)
{
failures.Add(exception);
}
}
if (failures.Count == 1)
ExceptionDispatchInfo.Capture(failures[0]).Throw();
if (failures.Count > 1)
throw new AggregateException(failures);
return Task.CompletedTask;
}
public void FlushAllBackingStores()
public void FlushAllBackingStores() => Held(() =>
{
lock (_gate)
foreach (var (store, _) in _stores)
{
foreach (var (store, _) in _stores)
{
if (!store.IsReadOnly && store.IsDirty)
store.PushAll();
}
if (!store.IsReadOnly && store.IsDirty)
store.PushAll();
}
}
return true;
});
// soft is dead. CultCacheStudioWindow reflects on FlushAsync(bool) until Cut 6 rewrites it.
public Task FlushAsync(bool soft = false)
@ -1428,11 +1442,12 @@ namespace GameCult.Caching
public void Dispose()
{
lock (_gate)
Held(() =>
{
if (FlushAttachedStoresOnDispose && IsDirty)
FlushAllBackingStores();
}
return true;
});
foreach (var store in BackingStores)
store.Dispose();
@ -1553,6 +1568,8 @@ namespace GameCult.Caching
CacheBackingStore? source,
Func<CacheBackingStore?, CultCommitOutcome> land) => Held(() =>
{
if (_held == null)
throw new InvalidOperationException("An admission reached the cache under a plain lock on its gate; every admission runs in a hold.");
var outcome = land(Validate(admitted, evicted, source));
if (outcome != CultCommitOutcome.Committed)
return outcome;
@ -1569,6 +1586,9 @@ namespace GameCult.Caching
{
if (Monitor.IsEntered(_gate))
return body();
if (_held != null)
throw new InvalidOperationException(
"A cache hold was entered inside another cache's hold; a thread holds one cache's gate at a time.");
var outer = _held;
var mine = _held = new List<(CultCache Cache, Change Change, bool Loaded)>();
T result;
@ -1596,17 +1616,20 @@ namespace GameCult.Caching
private static List<Exception> Deliver(List<(CultCache Cache, Change Change, bool Loaded)> changes)
{
var failures = new List<Exception>();
void Try(Action observe)
{
try { observe(); }
catch (Exception exception) { failures.Add(exception); }
}
foreach (var (cache, change, loaded) in changes)
{
Try(() => cache._changes.OnNext(change));
if (loaded)
Try(() => cache.OnUpdate?.Invoke(change.Previous, change.Document));
// R3 routes a throwing subscriber to its unhandled-exception handler; OnNext does not throw.
cache._changes.OnNext(change);
if (!loaded)
continue;
try
{
cache.OnUpdate?.Invoke(change.Previous, change.Document);
}
catch (Exception exception)
{
failures.Add(exception);
}
}
return failures;