Deliver each cache's changes in save order

The outermost hold takes a ticket under the gate and, after releasing it,
waits on a per-cache delivery gate for its turn, delivers, then advances
the turn. Delivery order across threads equals admission order. A hold
closed while its thread is delivering queues its ticket behind that
delivery on the same thread instead of waiting, so an observer writing
the cache cannot deadlock against a writer: a waiting thread holds no gate
and no turn, and a turn holder never waits.

Hand-over-hand from the gate to a delivery lock was rejected: a writer
blocked on the delivery lock while holding the gate deadlocks with an
observer that writes the same cache.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-13 20:09:23 +02:00
commit bcae4839d0
3 changed files with 165 additions and 31 deletions

View file

@ -49,6 +49,16 @@ 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.
- Changes are delivered in save order. Across threads, a cache delivers changes
in the order it admitted them, so a later change to a key never reaches an
observer before an earlier one. A call whose changes were admitted after
another call's waits, outside the gate, until that delivery finishes.
- A write made by an observer during delivery is not delivered before that
write returns. It is queued behind the delivery in progress, in save order,
and delivered on the same thread before the outermost call returns; its
`OnUpdate` exceptions are rethrown to that outermost caller.
- An observer must not block waiting on another thread's write to a cache:
that thread's delivery may be waiting for this one to finish.
- 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