diff --git a/docs/MxAccessWorkerInstanceDesign.md b/docs/MxAccessWorkerInstanceDesign.md index 2885b7bf..24c5216f 100644 --- a/docs/MxAccessWorkerInstanceDesign.md +++ b/docs/MxAccessWorkerInstanceDesign.md @@ -746,7 +746,13 @@ the handle they receive when the registry does not already track it. An `Unregister` ran is dispatched after the eviction and recreates the entry; when MXAccess later reuses that item id, `ReadBulk`'s cached path would otherwise serve the stale value as `was_cached = true`. A handle the -registry still tracks belongs to a live item, so its entries are kept. Failed advice and cleanup calls +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 leave registry state unchanged so diagnostics continue to reflect the last successful MXAccess-owned state transition. diff --git a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/MxAccessSessionTests.cs b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/MxAccessSessionTests.cs index 1b8a2bc3..7f5a5763 100644 --- a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/MxAccessSessionTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/MxAccessSessionTests.cs @@ -530,6 +530,65 @@ public sealed class MxAccessSessionTests AssertCallerItemIntact(session, serverHandle, callerHandle, "Tag.AddedOnly", expectAdvised: false); } + /// + /// UnAdvise drops the item's cached value: the item stays added but the + /// value stops updating, so a later re-advise must not be answered from + /// the previous advice's cache before its own first OnDataChange. + /// + [Fact] + public void UnAdvise_EvictsTheItemsCachedValueButKeepsTheItem() + { + RecordingMxAccessServer server = new(); + MxAccessValueCache valueCache = new(); + using MxAccessSession session = MxAccessSession.CreateForTesting(server, new NoopEventSink(), valueCache: valueCache); + int serverHandle = session.Register("client"); + int handle = session.AddItem(serverHandle, "Tag.Readvise"); + session.Advise(serverHandle, handle); + valueCache.Set(serverHandle, handle, BuildDataChange(serverHandle, handle, 3)); + + session.UnAdvise(serverHandle, handle); + + Assert.False(valueCache.TryGet(serverHandle, handle, out _)); + Assert.True(session.HandleRegistry.ContainsItemHandle(serverHandle, handle)); + } + + /// + /// The value a ReadBulk's temporary advice on a caller's added-only item + /// produced must not be served as cached once the caller advises the + /// item itself: the next read waits for the caller's own first value. + /// + [Fact] + public void ReadBulk_AfterTemporaryAdviceOnAddedOnlyItem_LaterAdviseIsNotServedTheTemporaryValue() + { + RecordingMxAccessServer server = new() { ReuseHandleForSameTag = true }; + MxAccessValueCache valueCache = new(); + using MxAccessSession session = MxAccessSession.CreateForTesting(server, new NoopEventSink(), valueCache: valueCache); + int serverHandle = session.Register("client"); + int callerHandle = session.AddItem(serverHandle, "Tag.Seed"); + int nextValue = 17; + Action pumpStep = () => + { + if (server.TryGetAdvisedHandle("Tag.Seed", out int handle) && !valueCache.TryGet(serverHandle, handle, out _)) + { + valueCache.Set(serverHandle, handle, BuildDataChange(serverHandle, handle, nextValue)); + } + }; + + BulkReadResult seed = Assert.Single( + session.ReadBulk(serverHandle, new[] { "Tag.Seed" }, TimeSpan.FromSeconds(5), pumpStep)); + Assert.Equal(17, seed.Value.Int32Value); + + session.Advise(serverHandle, callerHandle); + nextValue = 99; + BulkReadResult afterAdvise = Assert.Single( + session.ReadBulk(serverHandle, new[] { "Tag.Seed" }, TimeSpan.FromSeconds(5), pumpStep)); + + Assert.True(afterAdvise.WasSuccessful); + Assert.False(afterAdvise.WasCached); + Assert.Equal(99, afterAdvise.Value.Int32Value); + AssertCallerItemIntact(session, serverHandle, callerHandle, "Tag.Seed", expectAdvised: true); + } + /// /// Defence in depth for the ordinal registry lookup: when a snapshot /// AddItem for an address the registry does not know comes back with a diff --git a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessSession.cs b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessSession.cs index 1265bb1b..f85b528f 100644 --- a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessSession.cs +++ b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessSession.cs @@ -350,6 +350,12 @@ public sealed class MxAccessSession : IDisposable mxAccessServer.UnAdvise(serverHandle, itemHandle); handleRegistry.RemoveAdviceHandles(serverHandle, itemHandle); + // The item stays added but its last value stops updating. Drop it so + // a later re-advise is not answered by ReadBulk's cached fast path + // with the value from the previous advice (including a ReadBulk's + // own temporary advice on a caller's added-only item) before the new + // advice's first OnDataChange lands. + valueCache.Remove(serverHandle, itemHandle); } /// Advises on item changes with supervisory subscription.