fix(opcua): replay writes deferred during an off-thread plan; cache-ref and guard fixes (Lane B fix round 1)

C1 (critical): B6 computes the plan off the pinned thread while B1 made writes
non-creating, so a value/status/alarm write for a node the in-flight plan adds
was skipped, and change-driven drivers never republish it (BadWaitingForInitialData
indefinitely; on a full rebuild every re-subscribe value landed on discarded
nodes). OpcUaPublishActor now applies writes immediately AND remembers the
latest per (realm, id) while a plan is in flight or queued (value+status
compose; alarm state/quality coalesce to the latest, which the node's delta
gate makes inert on an existing condition), replays it after each apply before
answering, and drops it once the pipeline is idle. Bounded (250k keys by
default; beyond it new keys are not held, logged once).

I2: the data-plane-authority parse runs in its own try inside the plan task; a
throw is logged and never degrades the address-space apply.

I3: the SDK's DeleteMonitoredItems releases an item's component-cache
reference on a Good delete; a tombstoned item's reference was already
released, so the delete took one from the re-created node's entry. Released
items are remembered and the orphan delete takes one back first. Event items
never held a reference (EventManager + SubscribeToEvents), so the three
event-item releases are gone.

I4: direct tests for the HistoryUpdate override's per-request updater check
and the HistoryReadEvents override's source guard (both reached by restoring
the bit after a reset - the B7 race window); fixed the false remark.

Minors: the MaterialiseAlarmCondition kind-swap tombstones the dropped
instance; a bytes-less rebuild arriving mid-plan queues behind it (replies in
order); a PlanReady/PlanFailed from a previous actor incarnation is ignored and
its requester answered degraded; the tie-cluster end bound of an uncapped read
fits against the backend ceiling (defence in depth - guarded by a sweep test);
the lock-time benchmark stays in the default run, headroom noted.

Refs #732
Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
Joseph Doherty
2026-10-22 14:30:11 -04:00
parent 7c7ecff974
commit f966efcfad
9 changed files with 977 additions and 30 deletions
@@ -139,6 +139,10 @@ public sealed class StaleMonitoredNodeWireTests
"the new item must receive a value written after its creation");
lock (gate) newValues.ShouldNotContain(v => v.StatusCode == StatusCodes.BadNodeIdUnknown);
// Review fix I3: the cache entry the new item holds is ITS reference alone — the old item's was released
// by the removal.
CacheRefCount(nm, nodeId).ShouldBe(1, "one live item, one component-cache reference");
// The old item was told, and the dead node's fan-out is gone.
await WaitUntilAsync(() => { lock (gate) return oldValues.Any(v => v.StatusCode == StatusCodes.BadNodeIdUnknown); }, TimeSpan.FromSeconds(5),
"the item on the removed node must be told BadNodeIdUnknown");
@@ -150,9 +154,18 @@ public sealed class StaleMonitoredNodeWireTests
(oldItem.Status.Error is null || ServiceResult.IsGood(oldItem.Status.Error)).ShouldBeTrue(
$"deleting the orphaned item must succeed, got {oldItem.Status.Error}");
// The SDK releases a deleted item's cache reference on a Good delete; the old item's was released by the
// removal already, so a second release would evict the NEW item's live entry (review fix I3).
CacheRefCount(nm, nodeId).ShouldBe(1, "deleting the orphaned item must not take the new item's reference");
// And the new item is unaffected by that delete.
nm.WriteValue(Tag, 44, OpcUaStatus.Good, DateTime.UtcNow, AddressSpaceRealm.Raw);
await WaitUntilAsync(() => { lock (gate) return newValues.Any(v => Equals(v.Value, 44)); }, TimeSpan.FromSeconds(5));
// Positive control: deleting the live item gives its reference back.
subscription.RemoveItem(newItem);
await subscription.ApplyChangesAsync(ct);
CacheRefCount(nm, nodeId).ShouldBe(0);
}
finally
{
@@ -160,6 +173,28 @@ public sealed class StaleMonitoredNodeWireTests
}
}
/// <summary>The SDK's component-cache reference count for <paramref name="nodeId"/> (private SDK state, read by
/// reflection: <c>CustomNodeManager2.m_componentCache</c> → <c>CacheEntry.RefCount</c>); 0 when no entry.</summary>
private static int CacheRefCount(OtOpcUaNodeManager nm, NodeId nodeId)
{
const System.Reflection.BindingFlags Any =
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;
var field = typeof(Opc.Ua.Server.CustomNodeManager2).GetField("m_componentCache", Any)
.ShouldNotBeNull("the SDK's component cache field (SDK internals moved?)");
lock (nm.Lock)
{
if (field.GetValue(nm) is not System.Collections.IDictionary cache || !cache.Contains(nodeId)) return 0;
var entry = cache[nodeId]!;
var member = (object?)entry.GetType().GetField("RefCount", Any) ?? entry.GetType().GetProperty("RefCount", Any);
return member switch
{
System.Reflection.FieldInfo f => (int)f.GetValue(entry)!,
System.Reflection.PropertyInfo p => (int)p.GetValue(entry)!,
_ => throw new InvalidOperationException("CacheEntry.RefCount not found"),
};
}
}
private static void Materialise(OtOpcUaNodeManager nm)
{
nm.EnsureFolder(Device, null, "dev1", AddressSpaceRealm.Raw);