From e4740df2c99a1c31b763edfb9452309cd590867d Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Wed, 2 Dec 2026 15:36:52 -0500 Subject: [PATCH] fix(opcua): #970 a projected lookup racing back-to-back renewals never gives up and reports a live node missing #968 made OtOpcUaNodeManager.TryResolve/TryResolveForRenewal re-evaluate a definition replaced mid-check, but at most 16 times. A reader preempted while a renewer swapped the definition back to back exhausted the bound and answered 'no such node' for a live node, which tears a physical stream down (A_definition_replaced_by_a_renewal_never_resolves_as_missing failed 22/280 under load, on master too). The bound is removed; the double read stays (it is what stops a removed node being answered). The loop provably ends: the committed map compares values by reference, so a retry happens only when a replacement of THIS node was committed during that pass. Iterations are at most 1 + the replacements committed during the call (lock-free progress; commits are serialized under Lock and each needs a renewal or page apply); a removed node or a disposing manager ends it at once. Regression: A_lookup_outlasting_many_back_to_back_replacements_never_resolves_as_missing (deterministic: the definition's authority check commits a replacement each time it is evaluated, 64 times in a row; red on master, green now). Stress at the same load: 280/280 runs of the 5 renewal tests pass (master: 22/280 failed). --- docs/DataConnections.md | 5 ++- .../OtOpcUaNodeManager.cs | 15 +++++-- ...ataMetadataRenewalConcurrentCommitTests.cs | 42 +++++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/docs/DataConnections.md b/docs/DataConnections.md index 73a774a08..3aa3ef51e 100644 --- a/docs/DataConnections.md +++ b/docs/DataConnections.md @@ -156,7 +156,10 @@ reads, so a stream racing a renewal could see its live node as gone for an insta stream down (a freshness check breaking the attempt, or `Source unavailable` on the rebind's baseline). Two cadences of the same node made it likely, because both renew on the same tick and the second (redundant) renewal replaces the definition again while the first cadence rebinds. A lookup now -re-evaluates a definition replaced mid-check instead of reporting it missing. +re-evaluates a definition replaced mid-check instead of reporting it missing. Since #970 it does so with no +fixed bound (the first cut gave up after 16 back-to-back replacements, which a preempted reader racing a +fast renewer could hit). It retries only when the node's entry was actually replaced during that pass, so it +ends once replacements stop, and a removed node or a disposing server still ends it at once. Since #969 cadences of the same node no longer renew it twice: a cadence that reaches the renewal window while another cadence is already renewing the same predecessor waits for that renewal (one origin diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs index b53d787de..146b3636d 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs @@ -4431,7 +4431,9 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage // #968: the definition is re-read after its checks so a stale one is never answered. A renewal that REPLACED it in // between is not a missing node: evaluate the replacement. Answering false there made every subscription demand // check racing a metadata renewal see a live node as gone and tear its physical stream down. - for (var attempt = 0; attempt < MaxProjectedResolveAttempts; attempt++) + // #970: no fixed retry bound (16 gave up on a reader preempted across back-to-back replacements); see + // ProjectedResolveTerminates for why the loop ends. + while (true) { if (_disposing || !Volatile.Read(ref _committedProjectedDefinitions).TryGetValue(nodeId, out var current)) break; var usable = current.Lifetime.IsActive && current.AuthorityIsCurrent() && !_disposing && current.Lifetime.IsActive; @@ -4445,8 +4447,13 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage return false; } - /// #968: replacements a lock-free projected lookup re-evaluates before giving up (renewals are seconds apart). - private const int MaxProjectedResolveAttempts = 16; + // ProjectedResolveTerminates (#970): TryResolve / TryResolveForRenewal re-read the node's entry after checking it and retry + // only when that entry is a DIFFERENT reference. The committed map compares values by reference (SetItem of the same + // definition is a no-op), so a retry happens only when a writer committed a replacement of THIS node during that + // iteration. Iterations are therefore at most 1 + the replacements of this node committed during the call: every retry + // is paid for by a completed commit (lock-free progress), and those commits are serialized under Lock and each needs a + // metadata renewal or page apply. A removed node or a disposing manager ends the loop at once. A fixed bound only + // traded that guarantee for a false "missing", which tears a live stream down. /// /// #922: like but WITHOUT the authority check, so a node whose metadata lease lapsed can be renewed @@ -4454,7 +4461,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage /// public bool TryResolveForRenewal(NodeId nodeId, out ProjectedVariableDefinition? definition) { - for (var attempt = 0; attempt < MaxProjectedResolveAttempts; attempt++) + while (true) // #970: terminates, see ProjectedResolveTerminates { if (_disposing || !Volatile.Read(ref _committedProjectedDefinitions).TryGetValue(nodeId, out var current)) break; var active = current.Lifetime.IsActive; diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataMetadataRenewalConcurrentCommitTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataMetadataRenewalConcurrentCommitTests.cs index 708475e6a..fd390f802 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataMetadataRenewalConcurrentCommitTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataMetadataRenewalConcurrentCommitTests.cs @@ -61,6 +61,48 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests }); } + /// + /// #970: the #968 lookup gave up after 16 back-to-back replacements and answered "no such node" for a live node (a + /// reader preempted while a renewer swapped the definition). A lookup now retries for as long as the node's entry keeps + /// being replaced — every retry is a committed replacement, so it ends when the replacements stop. Deterministic: the + /// definition's own authority check commits a replacement each time the lookup evaluates it, 64 times in a row. + /// + [Fact(Timeout = 90000), Trait("Phase", "P11Renewal")] + public async Task A_lookup_outlasting_many_back_to_back_replacements_never_resolves_as_missing() + { + LocalDataFixture? peer = null; + var subscriptions = new FakeSubscriptions(() => peer!.Serving); + using var origin = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (_, _) => subscriptions); + peer = origin; subscriptions.SampleClock = origin.Clock; + await WithComposedServer(origin, origin.Raw, async (session, sink, _, _, _, _, anchor) => + { + await BrowseAll((SessionClient)session, anchor, 1); + var lookup = (IProjectedNodeLookup)sink; + var pages = (IProjectedNamespacePageSink)sink; + lookup.TryResolve(anchor, out var initial).ShouldBeTrue(); + const int Swaps = 64; + bool armed = false, replacing = false; + var replacements = 0; + bool Authority() + { + if (armed && !replacing && replacements < Swaps) + { + replacing = true; // TryRenewExact evaluates the replacement's authority itself + try { if (pages.TryRenewExact(anchor, initial!, initial! with { AuthorityIsCurrent = Authority }, () => true)) replacements++; } + finally { replacing = false; } + } + return initial!.AuthorityIsCurrent(); + } + pages.TryRenewExact(anchor, initial!, initial! with { AuthorityIsCurrent = Authority }, () => true).ShouldBeTrue(); + armed = true; + lookup.TryResolve(anchor, out var resolved).ShouldBeTrue("a live node replaced 64 times mid-lookup is not missing"); + replacements.ShouldBe(Swaps, "the lookup must actually have raced every replacement"); + resolved.ShouldNotBeNull(); + lookup.TryResolve(anchor, out var final).ShouldBeTrue(); + final.ShouldBeSameAs(resolved); + }); + } + /// /// #968: both cadences reach the renewal window on the same tick and each renews the same predecessor. The second /// commit adopts the successor the first already committed (and replaces the node's definition once more) after the