fix(runtime): #1113 a config-change respawn keeps the comms latch, trimmed to the refs the new composition still routes

Item 2. StopChild cleared the comms-loss latch unconditionally, so a
config-change respawn of an already comms-lost driver never audited its
replacement (an operator Restart does, #1053). StopChild(respawning: true) now
keeps it; every apply's reindex trims every outstanding latch to the refs still
routed for that driver (TrimCommsLatchesToIndex) and drops an emptied one, so a
ref the deploy dropped (no longer subscribed, cannot be re-published) never
raises a false audit Warning. Both run inside the same apply message, before the
replacement's Connected can arm the audit.

Tests: DriverHostRespawnValueNodeGapTests — kept latch audits the silent
replacement for exactly the kept ref (fails if the latch is cleared or not
trimmed), and a replacement re-publishing every kept ref stays silent (fails
without the trim).
This commit is contained in:
Joseph Doherty
2026-12-05 03:03:43 -05:00
parent 5ef8089e1f
commit 77adf410df
3 changed files with 145 additions and 12 deletions
@@ -2364,7 +2364,7 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
{
if (!_commsLatched.TryGetValue(driverInstanceId, out var latched)) return;
if (!latched.Remove(fullReference)) return;
if (latched.Count == 0) _commsLatched.Remove(driverInstanceId);
if (latched.Count == 0) ForgetCommsLatch(driverInstanceId);
}
/// <summary>
@@ -2384,6 +2384,40 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
private static string CommsAuditTimerKey(string driverInstanceId) => $"comms-audit:{driverInstanceId}";
/// <summary>Drops <paramref name="driverInstanceId"/>'s comms-loss latch.</summary>
/// <param name="driverInstanceId">The driver whose latch ends.</param>
private void ForgetCommsLatch(string driverInstanceId)
{
_commsLatched.Remove(driverInstanceId);
}
/// <summary>
/// Gitea #1113 item 2: trims every outstanding comms-loss latch to the value refs the just-rebuilt index still
/// routes for that driver, so a ref a deploy dropped (or a respawn's new composition no longer carries) can never
/// be reported by the recovery audit — it cannot re-publish a ref nobody subscribes it to. A latch left empty is
/// dropped. Runs after every reindex (<see cref="ApplyDesiredSubscriptions"/>), which is also where a
/// config-change respawn's kept latch (<see cref="StopChild"/>) meets the new composition — before the new
/// incarnation's <c>Connected</c> can arm the audit, since both happen inside the same apply message.
/// </summary>
private void TrimCommsLatchesToIndex()
{
if (_commsLatched.Count == 0) return;
foreach (var driverId in _commsLatched.Keys.ToArray())
{
var latched = _commsLatched[driverId];
if (!_valueRefsByDriver.TryGetValue(driverId, out var entries) || entries.Count == 0)
{
ForgetCommsLatch(driverId);
continue;
}
var current = new HashSet<string>(entries.Count, StringComparer.Ordinal);
foreach (var (rawPath, _) in entries) current.Add(rawPath);
latched.IntersectWith(current);
if (latched.Count == 0) ForgetCommsLatch(driverId);
}
}
/// <summary>
/// The 03/S15 tripwire firing: refs this host latched <c>BadNoCommunication</c> that the driver has
/// still not re-published <see cref="_commsRecoveryAuditDelay"/> after it reconnected.
@@ -2406,7 +2440,7 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
{
if (!_commsLatched.TryGetValue(msg.DriverInstanceId, out var latched) || latched.Count == 0)
{
_commsLatched.Remove(msg.DriverInstanceId);
ForgetCommsLatch(msg.DriverInstanceId);
return;
}
@@ -2423,7 +2457,7 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
_localNode, msg.DriverInstanceId, driverType, latched.Count, _commsRecoveryAuditDelay,
latched.OrderBy(r => r, StringComparer.Ordinal).First());
_commsLatched.Remove(msg.DriverInstanceId);
ForgetCommsLatch(msg.DriverInstanceId);
}
/// <summary>
@@ -4503,6 +4537,9 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
_dataWriteRuntime.Routes.ReplaceRoutes(mapped);
}
// #1113 item 2: no comms latch may outlive the ref it names (a respawn's kept latch, or a kept driver's dropped ref).
TrimCommsLatchesToIndex();
// Gitea #979 (review 2026-12-02 R-6): a value ref that left the composition (raw tag deleted or renamed, or its
// driver removed) used to be only forgotten — a surviving VirtualTag / Calculation tag / scripted alarm that
// still reads it kept computing Good from its last value. Tell the mux, which hands every dependent naming it
@@ -5253,7 +5290,11 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
// behind, a removed-while-comms-lost driver kept its whole RawPath set forever, a removal within the audit
// window still fired a false Warning + otopcua.driver.comms_latched_refs increment, and a disable ->
// re-enable of the same id inherited the stale latch.
_commsLatched.Remove(driverInstanceId);
// #1113 item 2: EXCEPT on a config-change respawn — like an operator Restart (#1053), the replacement is what
// must re-publish those refs, so the latch is kept for its Connected to audit. This apply's reindex trims it to
// the refs the new composition still carries (TrimCommsLatchesToIndex), so a dropped ref is never reported.
// The old incarnation's armed audit is cancelled either way.
if (!respawning) ForgetCommsLatch(driverInstanceId);
Timers.Cancel(CommsAuditTimerKey(driverInstanceId));
_healthPublisher.Forget(driverInstanceId);
_log.Info("DriverHost {Node}: stopped driver child {Id}", _localNode, driverInstanceId);