fix(worker): ReadBulk never tears down a caller's item or subscription (#238)

ReadBulk sent every advised-but-uncached and added-but-not-advised tag
down the snapshot path. MXAccess returns the caller's existing handle for
a re-added address, so the snapshot's AddItem/Advise ran on the caller's
own item and the symmetric teardown then UnAdvised and RemoveItem'd it:
the subscription delivered one value and went silent.

Teardown is now ownership-scoped: it unadvises only advices the read's
own Advise created and removes only items the read's own AddItem created.
A tag the session already holds is read through the caller's handle with
no AddItem: advised-but-uncached waits on the existing subscription with
no COM call; added-but-not-advised is advised for the read and unadvised
afterwards (back to its prior state), never removed. A snapshot AddItem
that returns a handle the registry already tracks (differently spelled
address) is treated as the caller's item and never removed.

Adds MxAccessSessionTests regressions; updates gateway.md, Contracts,
DesignDecisions, MxAccessWorkerInstanceDesign.
This commit is contained in:
Joseph Doherty
2026-12-01 19:25:19 -05:00
parent 54367e7408
commit 7a41d9edea
8 changed files with 520 additions and 78 deletions
+7 -4
View File
@@ -88,10 +88,13 @@ of items per round trip while preserving MXAccess parity per entry.
`was_successful`, `was_cached`, `value`, `quality`, `source_timestamp`,
`repeated MxStatusProxy statuses`, and `error_message`. MXAccess has no
synchronous `Read`, so `ReadBulk` is dual-mode per entry: when a tag is
already advised in the session the worker returns the cached
`OnDataChange` payload without touching the subscription
(`was_cached = true`); otherwise the worker takes a full
`AddItem` + `Advise` + wait-for-first-`OnDataChange` + `UnAdvise` +
already advised in the session and has a cached value the worker returns
the cached `OnDataChange` payload without touching the subscription
(`was_cached = true`); a tag the session holds without a cached value is
read through the caller's own item handle — waiting on its existing
subscription, or advising an added-only item for the read and
unadvising it after — and is never removed; otherwise the worker takes a
full `AddItem` + `Advise` + wait-for-first-`OnDataChange` + `UnAdvise` +
`RemoveItem` snapshot lifecycle and returns the result
(`was_cached = false`). The asymmetry that `BulkReadResult` has no
`hresult` field is intentional — `ReadBulk` outcomes are timeout / cache
+23 -6
View File
@@ -532,7 +532,21 @@ choices were considered:
(`was_cached = true`). Otherwise it takes the full `AddItem + Advise +
wait-for-first-OnDataChange + UnAdvise + RemoveItem` lifecycle itself
(`was_cached = false`) and leaves the session exactly as it was before
the call. The cache lives on a per-session `MxAccessValueCache`,
the call. "Exactly as it was" is enforced by ownership, not by
symmetry: teardown undoes only the `AddItem` and `Advise` calls the read
itself made. A tag the session already holds is read through the
caller's item handle with no `AddItem` — an advised tag whose first
`OnDataChange` is not cached yet is waited on through the caller's
subscription, and an added-but-not-advised tag is advised for the read
and unadvised afterwards, which returns it to the caller's prior state;
neither is ever removed. Re-adding the caller's address instead returns
the caller's own handle from MXAccess, and the old symmetric teardown
then unadvised and removed the caller's item — a subscription that
delivered one value and went silent (#238). A snapshot `AddItem` that
still returns a handle the registry already tracks (an address spelled
differently from the caller's) is never removed either; at worst that
leaves one extra `AddItem` reference until the caller removes the item
or unregisters, which is preferable to deleting a live subscription. The cache lives on a per-session `MxAccessValueCache`,
populated by `MxAccessBaseEventSink` on every `OnDataChange` after the
event clears the outbound queue.
@@ -543,12 +557,15 @@ choices were considered:
The chosen behavior matches what callers actually want from "current
value" — a free read of an already-streaming tag, and a one-shot snapshot
otherwise — and never disturbs subscriptions the caller did not create.
otherwise — and never disturbs subscriptions the caller already holds.
The decision intentionally does NOT synthesize an `OnDataChange` event
from the snapshot path: the snapshot value reaches the caller through
`ReadBulk`'s reply payload only, not through the event stream. This
preserves the "Don't synthesize events" rule that scopes the rest of the
worker.
from the snapshot path: the worker adds no event of its own for the
snapshot value, which reaches the caller through `ReadBulk`'s reply
payload. The native `OnDataChange` MXAccess raises for a handle the read
advised still flows through the event sink like any other — including,
for an added-but-not-advised caller item, under the caller's own item
handle while the read holds it advised. This preserves the "Don't
synthesize events" rule that scopes the rest of the worker.
`ReadBulk`'s wait loop pumps Windows messages on the worker STA
(`StaRuntime.PumpPendingMessages`) on every poll iteration so the inbound
+13 -4
View File
@@ -355,10 +355,19 @@ 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 registry and the cached-and-advised fast path; (2) every
remaining tag gets an advised handle — a tag the session does not hold gets
the read's own `AddItem`, a tag it holds keeps the caller's item handle, and
the handle is advised only when nothing advises it yet; (3) one wait with a
shared deadline collects the first `OnDataChange` per handle and returns as
soon as all have delivered; (4) teardown, best-effort and ownership-scoped,
unadvises only the advices phase 2 created and removes only the items phase 2
added. The caller's items are therefore never removed and the caller's
subscriptions never unadvised: an advised-but-uncached caller tag is waited on
through the existing subscription with no COM call, and an added-only caller
item is advised for the read and unadvised afterwards, back to its prior
state (#238). A snapshot `AddItem` that returns a handle the registry already
tracks is treated as the caller's item for the same reason. 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