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).
This commit is contained in:
Joseph Doherty
2026-12-02 15:36:52 -05:00
parent 59f1eabc87
commit e4740df2c9
3 changed files with 57 additions and 5 deletions
@@ -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;
}
/// <summary>#968: replacements a lock-free projected lookup re-evaluates before giving up (renewals are seconds apart).</summary>
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.
/// <summary>
/// #922: like <see cref="TryResolve"/> 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
/// </summary>
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;