fix(worker): leave added-only caller items to a snapshot AddItem (#238)
ci / windows-x86 (push) Successful in 2m4s
ci / java (push) Failing after 6m50s
ci / portable (push) Failing after 9m33s

The live run on windev showed MXAccess hands a re-added address a new
handle, so reading a caller's added-but-not-advised item through the
caller's handle (temporary Advise/UnAdvise) touched the caller's item for
no reason. Only an advised-but-uncached caller tag now reuses the caller's
handle (waiting on its existing subscription). Every other tag takes a
snapshot AddItem; if that AddItem returns a handle the registry already
tracks, it is still treated as the caller's item and never removed.

Docs and tests updated to the live observation; the live test now logs
the snapshot handle MXAccess returned for the re-added address.
This commit is contained in:
Joseph Doherty
2026-12-01 19:38:11 -05:00
parent 2e6645ef92
commit cc33b063fa
7 changed files with 154 additions and 107 deletions
+4 -5
View File
@@ -90,11 +90,10 @@ of items per round trip while preserving MXAccess parity per entry.
synchronous `Read`, so `ReadBulk` is dual-mode per entry: when a tag is
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` +
(`was_cached = true`); a tag the session has advised without a cached
value yet is read by waiting on that existing subscription; 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
+18 -16
View File
@@ -534,19 +534,21 @@ choices were considered:
(`was_cached = false`) and leaves the session exactly as it was before
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`,
itself made (#238). An advised tag whose first `OnDataChange` is not
cached yet is read by waiting on the caller's own subscription, with no
`AddItem` at all. Any other tag gets a snapshot `AddItem`. The installed
MXAccess on windev hands a re-added address a new handle (observed
2026-12-01), so a caller's added-but-not-advised item is not touched and
the snapshot item is torn down on its own. The worker does not rely on
that: when the snapshot `AddItem` returns a handle the registry already
tracks, the handle is the caller's, and a symmetric teardown would
unadvise and remove the caller's item — a subscription that delivered
one value and went silent. Such a handle is advised for the read only
if nothing advises it yet, unadvised afterwards (back to the caller's
added-not-advised state), and never removed; at worst one extra
`AddItem` reference outlives the read 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.
@@ -562,9 +564,9 @@ The decision intentionally does NOT synthesize an `OnDataChange` event
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
advised still flows through the event sink like any other (under the
snapshot item handle, or under the caller's own handle in the fallback
above where `AddItem` returned it). 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
+16 -15
View File
@@ -356,18 +356,20 @@ 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 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
remaining tag gets an advised handle — a tag the caller has advised keeps the
caller's item handle and its existing subscription, any other tag gets the
read's own `AddItem`, and a 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 (#238). A snapshot `AddItem`
that returns a handle the registry already tracks is the caller's item: it is
advised for the read only if nothing advises it yet, unadvised afterwards,
and never removed. (The windev MXAccess install returns a new handle for a
re-added address, so in practice an added-only caller item is not touched;
the ownership check covers an install or address spelling that resolves to
the caller's handle.) 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
@@ -750,9 +752,8 @@ registry still tracks belongs to a live item, so its entries are kept.
Successful `UnAdvise` drops the handle pair's `MxAccessValueCache` entry too:
the item stays added, but its value stops updating, so without the eviction a
later re-advise would let `ReadBulk`'s cached path answer with the previous
advice's value (for example the value a `ReadBulk`'s own temporary advice on a
caller's added-only item captured) before the new advice's first
`OnDataChange` lands. Failed advice and cleanup calls
advice's value (including one a `ReadBulk`'s temporary advice on a caller's
handle captured) before the new advice's first `OnDataChange` lands. Failed advice and cleanup calls
leave registry state unchanged so diagnostics continue to reflect the last
successful MXAccess-owned state transition.