Publish each hold's own changes to its own caller

The cache-wide unpublished list let any thread drain and deliver another
thread's changes: a throwing observer dropped the rest of the queue and threw
into an unrelated writer, writes returned before their change was published,
and a third-party load could wait indefinitely.

Every admission now runs in a hold (Held). The outermost hold on a thread
collects exactly the changes it admits and publishes them after it leaves the
gate, before it returns, on its caller's thread; nested holds append to it.
Every change is delivered even when an observer throws; the first exception
(AggregateException for several) is then rethrown, and a failing body's own
exception wins over observer exceptions.

Stores take the hold instead of locking a shared Gate object, so a direct
PullAll on an attached store publishes its own load and the empty follow-up
Loaded hand-over is gone. Gate is replaced by protected Held.

Tests: OwnChangeIsPublishedBeforeWriteReturns and
ThrowingObserverDoesNotLoseOtherChanges fail on 4eacd87 and pass here. The
repull tests now record a write that reaches the gate while the pull is paused
instead of inferring readiness from thread state; both fail when the pull does
not hold the gate.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-13 19:46:18 +02:00
commit 544c087cf1
4 changed files with 284 additions and 209 deletions

View file

@ -38,13 +38,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
public override string ToString() => _manifestFile.FullName;
public override void PullAll()
{
lock (Gate)
PullAllCore();
// Outside the gate, an empty hand-over lets the cache publish what this pull admitted.
Loaded?.Invoke(Array.Empty<CultStoredDocument>(), Array.Empty<CultStoredDocument>());
}
public override void PullAll() => Held(PullAllCore);
private void PullAllCore()
{
@ -110,31 +104,31 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
public override void Push(CultStoredDocument entry)
{
ThrowIfReadOnly();
lock (Gate)
Held(() =>
{
Entries[entry.Key.Value] = entry;
_dirtyKeys[entry.Key.Value] = true;
_deletedKeys.TryRemove(entry.Key.Value, out _);
IsDirty = true;
}
});
}
public override void Delete(CultStoredDocument entry)
{
ThrowIfReadOnly();
lock (Gate)
Held(() =>
{
Entries.TryRemove(entry.Key.Value, out _);
_dirtyKeys.TryRemove(entry.Key.Value, out _);
_deletedKeys[entry.Key.Value] = true;
IsDirty = true;
}
});
}
public override CultCommitOutcome CommitBatch(CultCommitRequest request, bool wait)
{
ThrowIfReadOnly();
lock (Gate)
return Held(() =>
{
Directory.CreateDirectory(_manifestFile.DirectoryName!);
using var commitLease = AcquireCommitLease(wait);
@ -180,13 +174,13 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
IsDirty = wasDirty;
throw;
}
}
});
}
public override void PushAll()
{
ThrowIfReadOnly();
lock (Gate)
Held(() =>
{
if (!IsDirty)
return;
@ -194,7 +188,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
Directory.CreateDirectory(_manifestFile.DirectoryName!);
using var commitLease = AcquireCommitLease(wait: true);
WriteGeneration(ReadManifest());
}
});
}
// Runs under the commit lease: pages first, then the manifest that names them.