fix(opcua): #1041 a device confirmation arriving after a newer write's revert lands instead of being dropped

#979 follow-up 1: write A then B, B refused first (node reverts to the pre-A value P), the
device confirms A afterwards. The revert closed the overlap window and A's success was dropped
as an outcome from a closed window, so the node showed P while the device held A until the
next driver publication. EquipmentVariableState.CloseWindowAfterRevert now keeps the window's
still-pending older attempts (bounded like the window, carried across later reverts above the
confirmed floor); a late success of one lands its tuple (LateConfirm) when nothing touched the
node since the revert, or becomes the confirmed tuple of a window opened since. A driver
publication or a newer owner's success discards them.

#979 follow-up 2 (dependents keep last value during operator Restart / config respawn): not a
defect - the stopped child's PostStop comms-loss fan (03/S15) reaches the dependency mux and
hands dependents BadNoCommunication until the new driver object publishes. Pinned by two new
DriverHostActorSourceRemovedTests (identical-value recovery); mutation-checked: removing the
PostStop ConnectivityChanged fails both.
This commit is contained in:
Joseph Doherty
2026-12-03 15:10:17 -05:00
parent cc7155e5e9
commit 186b5d696d
5 changed files with 207 additions and 1 deletions
@@ -29,6 +29,14 @@ internal sealed class EquipmentVariableState(NodeState? parent, object ownerLock
private NodeTuple? _confirmed;
private long _confirmedToken;
// Gitea #1041 (#979 follow-up): attempts still pending when their window closed by a REVERT. A refused newer
// attempt reverts the node to the confirmed tuple, but an older attempt the device has not answered yet may still
// succeed — and then the device holds ITS value. Kept here (bounded like the window) so that late confirmation can
// land: shown on the node when nothing newer touched it since the revert, or advancing the confirmed tuple of a
// window opened since. A driver publication or the owner's success clears them (the node then holds a newer truth).
private readonly SortedDictionary<long, NodeTuple> _revertedAttempts = new();
private long _revertedRevision = -1;
internal readonly record struct WriteStamp(EquipmentVariableState Owner, long Token);
/// <summary>One (value, status, source timestamp) tuple of the node.</summary>
@@ -43,6 +51,10 @@ internal sealed class EquipmentVariableState(NodeState? parent, object ownerLock
Revert,
/// <summary>A failure superseded by a newer attempt or a driver publication.</summary>
Superseded,
/// <summary>#1041: a success of an attempt whose window a newer attempt's refusal already reverted, with nothing
/// newer on the node since — the device holds this attempt's value: show it
/// (<see cref="ApplyLateConfirmation"/>).</summary>
LateConfirm,
}
internal WriteStamp? ActiveWriteStamp => _activeWriteToken is { } token ? new(this, token) : null;
@@ -59,6 +71,40 @@ internal sealed class EquipmentVariableState(NodeState? parent, object ownerLock
_windowAttempts.Clear();
_confirmed = null;
_confirmedToken = 0;
_revertedAttempts.Clear();
}
/// <summary>
/// #1041: closes the window after the owning attempt <paramref name="failedToken"/> was refused and the node reverted
/// (caller holds <c>ownerLock</c> and writes the confirmed tuple to the node next). Every OLDER attempt of the window
/// that the device has not answered yet — and that is newer than what the window already confirmed — stays eligible
/// for a late confirmation (see <see cref="ResolveWriteOutcome"/>), together with such attempts left over from earlier
/// reverts.
/// </summary>
internal void CloseWindowAfterRevert(long failedToken)
{
var floor = _confirmedToken;
var survivors = _windowAttempts.Where(kv => kv.Key < failedToken && kv.Key > floor)
.Concat(_revertedAttempts.Where(kv => kv.Key > floor)).ToArray();
InvalidateWriteOwnership();
foreach (var (token, tuple) in survivors) _revertedAttempts[token] = tuple;
while (_revertedAttempts.Count > MaxWindowAttempts) _revertedAttempts.Remove(_revertedAttempts.Keys.First());
_revertedRevision = _mutationRevision;
}
/// <summary>#1041: lands a <see cref="WriteOutcomeAction.LateConfirm"/> tuple on the node (caller holds
/// <c>ownerLock</c> and notifies subscribers). A still-pending NEWER reverted attempt can follow it the same way.</summary>
internal void ApplyLateConfirmation(NodeTuple confirmed)
{
_mutationRevision++;
SetTuple(confirmed.Value, confirmed.Status, confirmed.Timestamp);
_revertedRevision = _mutationRevision;
}
private void DropRevertedThrough(long token)
{
while (_revertedAttempts.Count > 0 && _revertedAttempts.Keys.First() <= token)
_revertedAttempts.Remove(_revertedAttempts.Keys.First());
}
internal void PublishValue(Variant value, StatusCode status, DateTimeUtc timestamp)
@@ -88,14 +134,37 @@ internal sealed class EquipmentVariableState(NodeState? parent, object ownerLock
_windowAttempts.Clear();
_confirmed = null;
_confirmedToken = 0;
// Every reverted attempt is older than the owner the device just confirmed.
_revertedAttempts.Clear();
}
else if (inWindow && stamp.Token > _confirmedToken)
{
_confirmed = attempted;
_confirmedToken = stamp.Token;
}
else if (_revertedAttempts.Remove(stamp.Token, out var late))
{
// #1041: the device confirmed an attempt after a newer one's refusal reverted the node. Anything older
// still pending can no longer be the device's last accepted write.
DropRevertedThrough(stamp.Token);
if (_committedWriteToken is not null)
{
// A newer attempt owns the node: this is the device's state underneath it, so a refusal of that
// attempt must land here rather than on the pre-revert tuple.
if (stamp.Token > _confirmedToken)
{
_confirmed = late;
_confirmedToken = stamp.Token;
}
return WriteOutcomeAction.None;
}
if (_mutationRevision != _revertedRevision) return WriteOutcomeAction.None;
confirmed = late;
return WriteOutcomeAction.LateConfirm;
}
return WriteOutcomeAction.None;
}
_revertedAttempts.Remove(stamp.Token);
if (!owner || _confirmed is not { } settled) return WriteOutcomeAction.Superseded;
confirmed = settled;
return WriteOutcomeAction.Revert;
@@ -4072,6 +4072,16 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
_logger.LogDebug("OtOpcUaNodeManager: device confirmed write for {NodeId}", nodeId);
return;
}
if (action == EquipmentVariableState.WriteOutcomeAction.LateConfirm && variable is EquipmentVariableState late)
{
// Gitea #1041: a newer attempt's refusal reverted the node before the device confirmed this older
// one, and nothing has touched the node since — the device holds this value, so show it now instead
// of leaving the reverted tuple until the next driver publication.
late.ApplyLateConfirmation(confirmed);
variable.ClearChangeMasks(SystemContext, includeChildren: false);
_logger.LogDebug("OtOpcUaNodeManager: device confirmed write for {NodeId} after a newer write's revert; landed it", nodeId);
return;
}
if (action == EquipmentVariableState.WriteOutcomeAction.Superseded)
{
_logger.LogDebug("OtOpcUaNodeManager: ignored superseded write outcome for {NodeId}", nodeId);
@@ -4095,7 +4105,12 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
variable.ClearChangeMasks(SystemContext, includeChildren: false); // notify — the Bad blip
var prior = restoreValue ?? SdkValueBoundary.FromClr(priorValue, variable.DataType == DataTypeIds.Byte && variable.ValueRank == ValueRanks.OneDimension);
if (variable is EquipmentVariableState restored) restored.InvalidateWriteOwnership();
if (variable is EquipmentVariableState restored)
{
// #1041: a stamped revert keeps the window's still-pending older attempts eligible for a late landing.
if (writeStamp is { } revertedStamp) restored.CloseWindowAfterRevert(revertedStamp.Token);
else restored.InvalidateWriteOwnership();
}
variable.Value = prior;
variable.StatusCode = priorStatus;
variable.Timestamp = restoreTimestamp ?? now;