fix(runtime): #1070 a stopped incarnation's late ConnectivityChanged no longer overrules a connected replacement

Confirmed, deterministically: Context.Stop is asynchronous and messages from two children are unordered, so an operator
Restart's old child announced comms loss from PostStop AFTER the replacement had connected and pushed its initial values
(the old child reaches its stop only once a synchronous driver call returns). The host then stamped every value node
BadNoCommunication with nothing left to re-publish them, re-latched them and cancelled the new incarnation's recovery
audit - nodes stuck Bad, tripwire disarmed. Pre-fix the new test saw 2 status batches where 1 is correct.

Fix at the root, in DriverHostActor.OnDriverConnectivityChanged: IsStaleRetiredConnectivity drops, whole, any
Connected=true from a retired child, and its Connected=false once the current child of the same id has announced
Connected (new ChildEntry.Connected, set only from the current child's own edges). A retired Bad edge is still applied
while the replacement has not connected (or there is none) - the 03/S15 gap cover Restart/respawn rely on; same rule the
condition-signal tracker already applied. Senders that were never children are not filtered.

Tests (DriverHostStaleConnectivityTests): the race made deterministic by wedging the old child in a blocking
GetHostStatuses; positive control = a Force Reconnect of the current child must yield exactly one status batch before the
re-subscribe push. Companion: a retired Bad edge before the replacement connects still stamps Bad and the push recovers.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-12-03 16:40:11 -05:00
parent dface0441e
commit 90ae995bf4
4 changed files with 302 additions and 2 deletions
@@ -765,6 +765,12 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
/// <summary>When this child was spawned — the diagnostics' last-change time until it publishes health.</summary>
public DateTime SpawnedUtc { get; } = DateTime.UtcNow;
/// <summary>
/// The last connectivity edge THIS incarnation announced (false until its first <c>Connected=true</c>).
/// #1070: a stopped predecessor's late Bad edge is dropped once this is true.
/// </summary>
public bool Connected { get; set; }
// Convenience accessors for sites that don't need the full spec.
/// <summary>Gets the driver type name from the child's spec.</summary>
public string DriverType => Spec.DriverType;
@@ -2006,8 +2012,9 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
private void ForwardBatchToMux(DriverInstanceActor.AttributeValueBatchPublished msg)
{
// A burst from an incarnation this host already stopped: forwarding it would undo the SourceRemoved
// eviction (see _retiredChildren). Its ConnectivityChanged is NOT filtered — the respawn path relies on
// that Bad fan to cover the gap.
// eviction (see _retiredChildren). Its ConnectivityChanged is filtered only once the replacement has
// connected (#1070, IsStaleRetiredConnectivity) — until then the respawn path relies on that Bad fan to
// cover the gap.
if (_retiredChildren.Contains(Sender))
{
_log.Debug("DriverHost {Node}: dropped a {Count}-item value burst from a stopped incarnation of {Id}",
@@ -2125,6 +2132,17 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
/// </summary>
private void OnDriverConnectivityChanged(DriverInstanceActor.ConnectivityChanged msg)
{
if (IsStaleRetiredConnectivity(msg))
{
_log.Debug(
"DriverHost {Node}: dropped a stale ConnectivityChanged(connected={Connected}) for {Id} from a stopped incarnation ({Stale})",
_localNode, msg.Connected, msg.DriverInstanceId, Sender);
return;
}
if (_children.TryGetValue(msg.DriverInstanceId, out var announcing) && Equals(announcing.Actor, Sender))
announcing.Connected = msg.Connected;
if (msg.Connected && _acquisitionDemands.ContainsKey(msg.DriverInstanceId) &&
_children.TryGetValue(msg.DriverInstanceId, out var activeChild) && Equals(activeChild.Actor, Sender))
SendDesiredSubscriptions(activeChild.Actor, msg.DriverInstanceId);
@@ -2178,6 +2196,38 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
_localNode, msg.DriverInstanceId, msg.Connected ? "connected" : "disconnected", annotated, statusCode);
}
/// <summary>
/// #1070 — whether <paramref name="msg"/> comes from a child this host already stopped (a
/// <see cref="_retiredChildren"/> member) and would overrule the CURRENT incarnation's state.
/// </summary>
/// <remarks>
/// <para>
/// <c>Context.Stop</c> is asynchronous and messages from two different children are not ordered, so a
/// stopped child's <c>PostStop</c> comms-loss announcement can reach this host AFTER its replacement
/// connected and published its initial push — the old child only gets to its stop once whatever it is
/// running returns (a synchronous driver call, e.g. a health publish). Applied then, it stamped every value
/// node <c>BadNoCommunication</c> with nothing left to re-publish them, re-latched them and cancelled the
/// recovery audit the new incarnation had armed: nodes stuck Bad with the tripwire disarmed.
/// </para>
/// <para>
/// Stale, and dropped whole: any <c>Connected=true</c> from a retired child (it vouches for a connection
/// that has been torn down — applied, it would arm an audit and annotate conditions Good for a driver
/// that may not be connected yet), and its <c>Connected=false</c> once the current child of the same id
/// has announced it is connected. A retired child's Bad edge is still APPLIED while the replacement has
/// not connected (or there is none): that is the 03/S15 gap cover an operator Restart and a respawn rely
/// on, and the replacement's own push clears it. Same rule the condition-signal tracker already applies
/// (<c>OnConditionSignalConnectivity</c>). A sender that was never a child of this host is not filtered.
/// </para>
/// </remarks>
/// <param name="msg">The connectivity announcement; <c>Sender</c> is its producer.</param>
/// <returns><see langword="true"/> when the message must be dropped.</returns>
private bool IsStaleRetiredConnectivity(DriverInstanceActor.ConnectivityChanged msg)
{
if (!_retiredChildren.Contains(Sender)) return false;
if (msg.Connected) return true;
return _children.TryGetValue(msg.DriverInstanceId, out var current) && current.Connected;
}
/// <summary>
/// Archreview <b>03/S15</b> — the value-node half of the comms-loss fan-out: stamp
/// <see cref="OpcUaStatus.BadNoCommunication"/> on every raw NodeId <b>and</b> every referencing UNS