fix(worker): UnAdvise evicts the item's cached value (#238)
ci / windows-x86 (push) Successful in 1m51s
ci / java (push) Failing after 7m13s
ci / portable (push) Failing after 14m3s

The live repro showed the value a ReadBulk's temporary advice on a
caller's added-only item captured surviving the UnAdvise, so after the
caller advised the item ReadBulk answered was_cached=true with that value
before the caller's own first OnDataChange. The same staleness applied to
any client UnAdvise followed by a re-advise. Drop the entry on UnAdvise;
the item stays added.
This commit is contained in:
Joseph Doherty
2026-12-01 19:31:49 -05:00
parent 13e9a26d39
commit 2e6645ef92
3 changed files with 72 additions and 1 deletions
+7 -1
View File
@@ -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.
@@ -530,6 +530,65 @@ public sealed class MxAccessSessionTests
AssertCallerItemIntact(session, serverHandle, callerHandle, "Tag.AddedOnly", expectAdvised: false);
}
/// <summary>
/// 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.
/// </summary>
[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));
}
/// <summary>
/// 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.
/// </summary>
[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);
}
/// <summary>
/// Defence in depth for the ordinal registry lookup: when a snapshot
/// AddItem for an address the registry does not know comes back with a
@@ -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);
}
/// <summary>Advises on item changes with supervisory subscription.</summary>