ReadBulk snapshot teardown unadvises/removes the client's own subscription for uncached tags #238

Closed
opened 2026-11-21 16:42:08 -05:00 by dohertj2 · 4 comments
Owner

MxAccessSession.ReadBulk (src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessSession.cs, ~956–1231) can silently tear down a subscription the client already holds.

Mechanism

  • Phase 1 answers from the cache only when the tag is advised and has a cached value (TryGetCachedReadFor).
  • An advised tag whose first OnDataChange hasn't been cached yet, or a tag that is added but not advised, goes to the snapshot path:
    • AddItem on the same address returns the client's existing item handle (MXAccess returns the existing handle for an already-added address; asserted in WorkerLiveMxAccessSmokeTests.cs:557);
    • Advise runs on that handle and the path waits;
    • TearDownSnapshots then calls UnAdvise and RemoveItem on it, deleting the client's own item and advice with nothing logged.
  • The tag gets one value and never updates again. The worker's handle registry still believes the item is advised.

Impact (suspected): ScadaBridge subscribes to 810 tags and then immediately seed-reads them in 250-tag ReadBulk chunks on every reconnect. Any tag not yet cached at that moment loses its subscription. On wonder-app-vd03 the gateway event rate fell from ~355/s to ~240/s at the 2026-11-21 R1/R2 deploy (docs/deploy/2026-11-21-vd03-perf-r1-r2.md), with the source load unchanged per the Historian. The faster event path in the new build likely leaves more tags uncached when the seed read runs. The bug is present in the old build too.

Fix: ReadBulk must never UnAdvise or RemoveItem a handle that existed before its own snapshot AddItem. For an already-advised tag with no cached value, wait on the existing subscription instead. Add regression tests and a windev live repro. Then redeploy to vd03 and confirm the rate is back to ~355/s.

Related: #236.

`MxAccessSession.ReadBulk` (`src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessSession.cs`, ~956–1231) can silently tear down a subscription the client already holds. **Mechanism** - Phase 1 answers from the cache only when the tag is advised **and** has a cached value (`TryGetCachedReadFor`). - An advised tag whose first `OnDataChange` hasn't been cached yet, or a tag that is added but not advised, goes to the snapshot path: - `AddItem` on the same address returns the client's **existing** item handle (MXAccess returns the existing handle for an already-added address; asserted in `WorkerLiveMxAccessSmokeTests.cs:557`); - `Advise` runs on that handle and the path waits; - `TearDownSnapshots` then calls `UnAdvise` and `RemoveItem` on it, deleting the client's own item and advice with nothing logged. - The tag gets one value and never updates again. The worker's handle registry still believes the item is advised. **Impact (suspected):** ScadaBridge subscribes to 810 tags and then immediately seed-reads them in 250-tag `ReadBulk` chunks on every reconnect. Any tag not yet cached at that moment loses its subscription. On wonder-app-vd03 the gateway event rate fell from ~355/s to ~240/s at the 2026-11-21 R1/R2 deploy (`docs/deploy/2026-11-21-vd03-perf-r1-r2.md`), with the source load unchanged per the Historian. The faster event path in the new build likely leaves more tags uncached when the seed read runs. The bug is present in the old build too. **Fix:** ReadBulk must never `UnAdvise` or `RemoveItem` a handle that existed before its own snapshot `AddItem`. For an already-advised tag with no cached value, wait on the existing subscription instead. Add regression tests and a windev live repro. Then redeploy to vd03 and confirm the rate is back to ~355/s. Related: #236.
dohertj2 added the area:workerpriority:p1type:bug labels 2026-11-21 16:42:08 -05:00
Author
Owner

Fixed on fix/238-readbulk-teardown (pushed, not merged): 7a41d9ed, 13e9a26d, 2e6645ef, cc33b063.

  • Teardown: it only undoes the UnAdvise/RemoveItem calls the read itself made.
  • Advised tag with no cached value yet: the read waits on the client's subscription and makes no COM calls.
  • Added-only item: it gets a separate snapshot AddItem. If MXAccess ever returns the client's own handle, the read restores it to added-not-advised and never removes it.
  • UnAdvise: it now drops the item's cached value. The live run caught a stale cached value after re-advise.
  • Docs: gateway.md, docs/Contracts.md, docs/DesignDecisions.md and docs/MxAccessWorkerInstanceDesign.md updated.

Tests:

  • windev filtered worker tests (MxAccessSession/CommandExecutor/HandleRegistry): 83/83, including 8 new regression tests. The x86 build has 0 warnings, and the CI full worker-test run passed.
  • New live test ReadBulkOfClientHeldTag passes on windev.

But the mechanism didn't reproduce on windev. MXAccess returned a new handle for the re-added address, and the old worker left the client's advice intact. So this fix is defensive and probably not the vd03 355→240/s cause. That investigation moves to #239. The Rust worker divergences are filed as #240.

Closing. Merging to main is still pending.

Fixed on `fix/238-readbulk-teardown` (pushed, not merged): `7a41d9ed`, `13e9a26d`, `2e6645ef`, `cc33b063`. - **Teardown:** it only undoes the UnAdvise/RemoveItem calls the read itself made. - **Advised tag with no cached value yet:** the read waits on the client's subscription and makes no COM calls. - **Added-only item:** it gets a separate snapshot AddItem. If MXAccess ever returns the client's own handle, the read restores it to added-not-advised and never removes it. - **UnAdvise:** it now drops the item's cached value. The live run caught a stale cached value after re-advise. - **Docs:** `gateway.md`, `docs/Contracts.md`, `docs/DesignDecisions.md` and `docs/MxAccessWorkerInstanceDesign.md` updated. **Tests:** - windev filtered worker tests (MxAccessSession/CommandExecutor/HandleRegistry): 83/83, including 8 new regression tests. The x86 build has 0 warnings, and the CI full worker-test run passed. - New live test `ReadBulkOfClientHeldTag` passes on windev. **But the mechanism didn't reproduce on windev.** MXAccess returned a *new* handle for the re-added address, and the old worker left the client's advice intact. So this fix is defensive and probably not the vd03 355→240/s cause. That investigation moves to #239. The Rust worker divergences are filed as #240. Closing. Merging to `main` is still pending.
Author
Owner

Note from the 2026-12-02 review (MXA-006): this is closed but the fix (89e38a1c) is only on fix/readbulk-subscription-teardown, not merged to main at 54367e74. Landing it is part of #252.

Note from the 2026-12-02 review (MXA-006): this is closed but the fix (`89e38a1c`) is only on `fix/readbulk-subscription-teardown`, not merged to `main` at `54367e74`. Landing it is part of #252.
dohertj2 reopened this issue 2026-12-03 09:34:57 -05:00
Author
Owner

Reopened 2026-12-03 (decision D5): the fix 89e38a1c is not on main. Close this when Phase 1 (#252) merges.

Reopened 2026-12-03 (decision D5): the fix `89e38a1c` is not on `main`. Close this when Phase 1 (#252) merges.
Author
Owner

Merged to main at 68043411 (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA.

The fix (e934854a after rebase) landed with Phase 1 (#252), per D5.

Merged to `main` at `68043411` (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA. The fix (`e934854a` after rebase) landed with Phase 1 (#252), per D5.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dohertj2/mxaccessgw#238