fix(worker): skip cache writes for late callbacks on removed handles (MXA-004, #255)
ci / windows-x86 (push) Failing after 15m0s
ci / java (push) Failing after 15m13s
ci / portable (push) Failing after 15m21s
ci / windows-x86 (push) Failing after 15m0s
ci / java (push) Failing after 15m13s
ci / portable (push) Failing after 15m21s
An OnDataChange/OnWriteComplete already queued on the STA is dispatched after RemoveItem evicted the handle and re-created the cache entry; item handles are never reused within a registration, so it leaked until Unregister. MxAccessSession.Create now binds its handle registry to the production sink, which only caches callbacks for handles the registry still holds. The event is still forwarded. EvictStaleHandleState stays as the backstop for unbound sinks.
This commit is contained in:
@@ -95,6 +95,62 @@ public sealed class MxAccessBaseEventSinkTests
|
||||
Assert.Equal(timestamp, cached.SourceTimestamp.ToDateTime());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MXA-004: an OnDataChange or OnWriteComplete that MXAccess had already
|
||||
/// queued on the STA is dispatched after <c>RemoveItem</c> evicted the
|
||||
/// handle. The unbound sink (see <see cref="OnDataChange_ComCallback_PopulatesValueCache"/>)
|
||||
/// records such a callback for any handle, which re-creates an entry nothing
|
||||
/// evicts again because item handles are not reused within a registration.
|
||||
/// Once bound to the session's registry, the sink must still forward the
|
||||
/// event but leave both caches untouched for a removed handle.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void LateCallbacks_ForRemovedItemHandle_AreForwardedButNotCached()
|
||||
{
|
||||
MxAccessEventQueue queue = new();
|
||||
MxAccessValueCache cache = new();
|
||||
MxAccessWriteCompletionCache completionCache = new();
|
||||
MxAccessBaseEventSink sink = new(queue, new MxAccessEventMapper(), cache, completionCache);
|
||||
MxAccessHandleRegistry registry = new();
|
||||
registry.RegisterServerHandle(7, "client");
|
||||
registry.RegisterItemHandle(7, 21, "Tag.A", string.Empty, hasItemContext: false);
|
||||
sink.BindHandleRegistry(registry);
|
||||
MXSTATUS_PROXY[] statuses = Array.Empty<MXSTATUS_PROXY>();
|
||||
|
||||
registry.RemoveItemHandle(7, 21);
|
||||
sink.OnDataChange(7, 21, 1234, 192, new DateTime(2026, 5, 18, 9, 15, 0, DateTimeKind.Utc), ref statuses);
|
||||
sink.OnWriteComplete(7, 21, ref statuses);
|
||||
|
||||
Assert.Equal(2, queue.Count);
|
||||
Assert.False(cache.TryGet(7, 21, out _));
|
||||
Assert.Equal(0UL, completionCache.CurrentVersion(7, 21));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The registry check must not suppress caching for a handle the session
|
||||
/// still holds.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Callbacks_ForLiveItemHandle_AreCachedWhenRegistryBound()
|
||||
{
|
||||
MxAccessEventQueue queue = new();
|
||||
MxAccessValueCache cache = new();
|
||||
MxAccessWriteCompletionCache completionCache = new();
|
||||
MxAccessBaseEventSink sink = new(queue, new MxAccessEventMapper(), cache, completionCache);
|
||||
MxAccessHandleRegistry registry = new();
|
||||
registry.RegisterServerHandle(7, "client");
|
||||
registry.RegisterItemHandle(7, 21, "Tag.A", string.Empty, hasItemContext: false);
|
||||
sink.BindHandleRegistry(registry);
|
||||
MXSTATUS_PROXY[] statuses = Array.Empty<MXSTATUS_PROXY>();
|
||||
|
||||
sink.OnDataChange(7, 21, 1234, 192, new DateTime(2026, 5, 18, 9, 15, 0, DateTimeKind.Utc), ref statuses);
|
||||
sink.OnWriteComplete(7, 21, ref statuses);
|
||||
|
||||
Assert.True(cache.TryGet(7, 21, out MxAccessValueCache.CachedValue cached));
|
||||
Assert.Equal(1234, cached.Value.Int32Value);
|
||||
Assert.Equal(1UL, completionCache.CurrentVersion(7, 21));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the sink-bound <c>ValueCache</c> is exposed for sharing with
|
||||
/// the owning <see cref="MxAccessSession"/> so writes and reads see the same
|
||||
|
||||
@@ -11,6 +11,7 @@ public sealed class MxAccessBaseEventSink : IMxAccessEventSink, IWriteCompletion
|
||||
private readonly MxAccessEventQueue eventQueue;
|
||||
private readonly MxAccessValueCache valueCache;
|
||||
private readonly MxAccessWriteCompletionCache writeCompletionCache;
|
||||
private MxAccessHandleRegistry? handleRegistry;
|
||||
private LMXProxyServerClass? server;
|
||||
private string sessionId = string.Empty;
|
||||
|
||||
@@ -88,6 +89,24 @@ public sealed class MxAccessBaseEventSink : IMxAccessEventSink, IWriteCompletion
|
||||
/// <inheritdoc />
|
||||
public MxAccessWriteCompletionCache WriteCompletionCache => writeCompletionCache;
|
||||
|
||||
/// <summary>
|
||||
/// Binds the session's handle registry so the per-handle caches only
|
||||
/// record callbacks for item handles the session still holds. MXAccess
|
||||
/// can dispatch an <c>OnDataChange</c> or <c>OnWriteComplete</c> that
|
||||
/// was already queued on the STA after <c>RemoveItem</c> evicted the
|
||||
/// handle; without this check the callback re-creates the entry, and
|
||||
/// because MXAccess never reuses an item handle within a registration,
|
||||
/// nothing evicts it again before <c>Unregister</c> (MXA-004). The event
|
||||
/// itself is still forwarded: the gateway relays every event MXAccess
|
||||
/// raises. Registry and callbacks are both STA-only, so no lock is
|
||||
/// needed. An unbound sink (unit tests) caches every callback.
|
||||
/// </summary>
|
||||
/// <param name="registry">The owning session's handle registry.</param>
|
||||
internal void BindHandleRegistry(MxAccessHandleRegistry registry)
|
||||
{
|
||||
handleRegistry = registry ?? throw new ArgumentNullException(nameof(registry));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Attach(
|
||||
object mxAccessComObject,
|
||||
@@ -153,7 +172,13 @@ public sealed class MxAccessBaseEventSink : IMxAccessEventSink, IWriteCompletion
|
||||
pwItemQuality,
|
||||
pftItemTimeStamp,
|
||||
statuses),
|
||||
mxEvent => valueCache.Set(hLMXServerHandle, phItemHandle, mxEvent));
|
||||
mxEvent =>
|
||||
{
|
||||
if (IsLiveItemHandle(hLMXServerHandle, phItemHandle))
|
||||
{
|
||||
valueCache.Set(hLMXServerHandle, phItemHandle, mxEvent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -180,7 +205,13 @@ public sealed class MxAccessBaseEventSink : IMxAccessEventSink, IWriteCompletion
|
||||
hLMXServerHandle,
|
||||
phItemHandle,
|
||||
statuses),
|
||||
mxEvent => writeCompletionCache.Record(hLMXServerHandle, phItemHandle, mxEvent.Statuses));
|
||||
mxEvent =>
|
||||
{
|
||||
if (IsLiveItemHandle(hLMXServerHandle, phItemHandle))
|
||||
{
|
||||
writeCompletionCache.Record(hLMXServerHandle, phItemHandle, mxEvent.Statuses);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -235,6 +266,14 @@ public sealed class MxAccessBaseEventSink : IMxAccessEventSink, IWriteCompletion
|
||||
statuses));
|
||||
}
|
||||
|
||||
private bool IsLiveItemHandle(
|
||||
int serverHandle,
|
||||
int itemHandle)
|
||||
{
|
||||
MxAccessHandleRegistry? registry = handleRegistry;
|
||||
return registry is null || registry.ContainsItemHandle(serverHandle, itemHandle);
|
||||
}
|
||||
|
||||
private void EnqueueEvent(Func<Proto.MxEvent> createEvent)
|
||||
{
|
||||
EnqueueEvent(createEvent, postPublish: null);
|
||||
|
||||
@@ -159,9 +159,19 @@ public sealed class MxAccessSession : IDisposable
|
||||
// ReadBulk reads both see the same instance. Fall back to a fresh
|
||||
// cache for test fakes that supply their own sink — ReadBulk simply
|
||||
// never serves cached values in that case.
|
||||
MxAccessValueCache valueCache = eventSink is MxAccessBaseEventSink baseSink
|
||||
? baseSink.ValueCache
|
||||
: new MxAccessValueCache();
|
||||
MxAccessHandleRegistry handleRegistry = new();
|
||||
MxAccessValueCache valueCache;
|
||||
if (eventSink is MxAccessBaseEventSink baseSink)
|
||||
{
|
||||
valueCache = baseSink.ValueCache;
|
||||
// Late callbacks for removed handles must not re-create cache
|
||||
// entries nothing would ever evict (MXA-004).
|
||||
baseSink.BindHandleRegistry(handleRegistry);
|
||||
}
|
||||
else
|
||||
{
|
||||
valueCache = new MxAccessValueCache();
|
||||
}
|
||||
|
||||
// Share the sink's completion cache the same way (the production
|
||||
// sink and completion-aware test sinks implement the provider
|
||||
@@ -176,7 +186,7 @@ public sealed class MxAccessSession : IDisposable
|
||||
mxAccessComObject,
|
||||
new MxAccessComServer(mxAccessComObject),
|
||||
eventSink,
|
||||
new MxAccessHandleRegistry(),
|
||||
handleRegistry,
|
||||
valueCache,
|
||||
writeCompletionCache,
|
||||
Environment.CurrentManagedThreadId);
|
||||
@@ -286,7 +296,9 @@ public sealed class MxAccessSession : IDisposable
|
||||
/// reused id would let ReadBulk's cached fast path serve that stale
|
||||
/// value as <c>was_cached = true</c>. A handle the registry still
|
||||
/// tracks is a live item (MXAccess returned an existing handle), so its
|
||||
/// current entries are kept.
|
||||
/// current entries are kept. The production sink now skips cache
|
||||
/// writes for handles the registry no longer holds (MXA-004), so this
|
||||
/// is the backstop for sinks that are not bound to the registry.
|
||||
/// </summary>
|
||||
private void EvictStaleHandleState(
|
||||
int serverHandle,
|
||||
|
||||
Reference in New Issue
Block a user