fix(worker): ReadBulk never tears down the caller's own item or advice (#238)

ReadBulk served a tag from the cache only when it was advised and cached;
every other tag took the AddItem + Advise + wait + UnAdvise + RemoveItem
snapshot lifecycle. If AddItem ever returned the caller's existing handle,
the teardown would unadvise and remove the caller's subscription.

ReadBulk now resolves a tag the session already holds to the session's
own item handle in phase 1:
- advised but uncached: wait on the caller's subscription for its first
  OnDataChange (baseline taken in phase 1, before any phase-2 pump can
  dispatch it); no COM call, no teardown.
- added but not advised: advise temporarily, then unadvise only; the item
  stays registered, and the value the temporary advice cached is dropped
  so it cannot later be served as was_cached = true.
- not held: unchanged snapshot lifecycle. An AddItem that returns a handle
  the registry already tracks is treated as the caller's (not
  re-registered, not removed).
Duplicate sharing, pump cadence, the shared deadline, timeout reporting
and was_cached semantics are unchanged.

Live repro on the windev rig (330 advised + 40 added-only tags, immediate
250-tag ReadBulk chunks x4 parallel, 60 s observation): unfixed and fixed
workers both keep 290/330 handles delivering. The 40 silent handles are
TestHistoryValue, which does not change within the window on either
build. The installed MXAccess hands out a distinct handle per AddItem
even for an address already added, and UnAdvise/RemoveItem of the second
handle leaves the first delivering. So the #238 teardown path does not
silence subscriptions on this MXAccess, and this change does not explain
the vd03 rate drop. The fix stays as a guard, and it saves an extra item
and four COM calls per uncached held tag.

Docs: gateway.md, Contracts.md, DesignDecisions.md,
MxAccessWorkerInstanceDesign.md and the five client ReadBulk doc comments.
This commit is contained in:
Joseph Doherty
2026-11-21 16:49:37 -05:00
parent 90d2d4a9b1
commit e934854a89
11 changed files with 470 additions and 59 deletions
+17 -6
View File
@@ -355,18 +355,29 @@ timeout.
`ReadBulk` runs its snapshot lifecycle in phases so the STA is held for at most
one `timeout_ms` per command, not one per tag: (1) every tag is validated and
probed against the cached-and-advised fast path; (2) every remaining tag is
added and advised; (3) one wait with a shared deadline collects the first
`OnDataChange` per snapshot handle and returns as soon as all have delivered;
(4) every snapshot handle is unadvised and removed, best-effort. Per-tag
probed against the cached-and-advised fast path, and a tag the session
already added is resolved to the session's own item handle; (2) every
remaining tag gets an advised handle to wait on — the session's advised
handle is used as is, the session's unadvised handle is advised
temporarily, and only a tag the session never added gets ReadBulk's own
`AddItem` and `Advise`; (3) one wait with a shared deadline collects the
first `OnDataChange` per handle and returns as soon as all have delivered;
(4) ReadBulk undoes only what phase 2 did, best-effort: it unadvises the
advices it made and removes the items it added, never the session's own
item or advice (#238). A session handle ReadBulk advised temporarily also
has its cached value dropped on unadvise, so a later `Advise` + `ReadBulk`
cannot serve that snapshot as `was_cached = true`. The baseline for a
session-advised handle is taken in phase 1, before any phase-2 pump can
dispatch its first `OnDataChange`, so that value still satisfies the read. Per-tag
results are unchanged: request order, the blank-address failure, the cached
result, an `AddItem` failure (item handle 0) or `Advise` failure (its own item
handle) failing only that tag, and the `ReadBulk timed out after <n> ms waiting
for first OnDataChange.` message for each tag that never delivered. The cache
probes all run before any snapshot `AddItem`, so a later tag in the same
request can never be served from an earlier tag's snapshot subscription.
When MXAccess returns the same item handle for two tags in one batch (a
duplicate address), the handle is advised, unadvised, and removed once, and
When two tags in one batch resolve to the same item handle (a duplicate
address of a session item, or an `AddItem` that returns a handle already in
play), the handle is advised, unadvised, and removed at most once, and
every duplicate receives the shared outcome. Phases 2 and 4 make one or two
COM calls per tag without waiting, so they call the pump step every
`ReadBulkPumpInterval` (16) tags; a large batch therefore keeps dispatching