fix(deploy): #1111 review — cover the held-ack retry guard; clamp deadline_remaining_ms off the wire

- The _pendingAcks.Count == 0 guard (a stale re-send of an older FAILED deployment must not
  re-apply it while a NEWER apply's Applied ack is held on its address-space outcome) had no
  test: mutating it to true left every Runtime test green. New test fails under that mutation
  (the older revision is re-fetched and applied over the newer one, whose held Applied ack
  then goes out for a rolled-back node).
- MeshTransportDtoMapper.FromProto clamps deadline_remaining_ms to [0, 1 day]: a long.MaxValue
  wire value threw from TimeSpan.FromMilliseconds; a hugely negative one could overflow the
  node's TimeSpan subtraction in HandleDispatchFromSteady (actor restart = remembered acks lost).
- Doc fix: _lastAnsweredDeploymentId is moved by RememberAck only, not by a replay.
This commit is contained in:
Joseph Doherty
2026-12-05 00:40:47 -05:00
parent f5485f57d8
commit 9be85f5a94
4 changed files with 70 additions and 4 deletions
@@ -80,10 +80,17 @@ public static class MeshTransportDtoMapper
CorrelationId.Parse(proto.CorrelationId),
// #1107: an empty repeated field is the original dispatch (null), never "addressed to nobody".
proto.TargetNodes.Count > 0 ? proto.TargetNodes.ToList() : null,
// #1111: absent (an original dispatch, or a pre-#1111 central) = null = the node's full default wait.
proto.HasDeadlineRemainingMs ? TimeSpan.FromMilliseconds(proto.DeadlineRemainingMs) : null);
// #1111: absent (an original dispatch, or a pre-#1111 central) = null = the node's full default wait. Clamped:
// the bound only ever SHORTENS the node's wait, and an out-of-range wire value must not throw here (a
// TimeSpan overflow) or overflow the node's tick arithmetic — negative is "spent", a day is "unbounded".
proto.HasDeadlineRemainingMs
? TimeSpan.FromMilliseconds(Math.Clamp(proto.DeadlineRemainingMs, 0L, MaxDeadlineRemainingMs))
: null);
}
/// <summary>#1111: the largest <c>deadline_remaining_ms</c> taken off the wire (one day — far past any apply deadline).</summary>
private const long MaxDeadlineRemainingMs = 24L * 60 * 60 * 1000;
// ---------------------------------------------------------------- RestartDriver
/// <summary>Projects <paramref name="message"/> onto its wire form.</summary>
@@ -372,8 +372,8 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn
private readonly Queue<DeploymentId> _recentAckOrder = new();
/// <summary>
/// #1111: the deployment this node sent its most recent ack for (any outcome, including a re-delivery's
/// re-ack of the same deployment). A re-send of a deployment whose remembered ack is <c>Failed</c> is
/// #1111: the deployment this node most recently PRODUCED an ack for (any outcome; set by
/// <see cref="RememberAck"/> — a replay of a remembered ack does not move it). A re-send of a deployment whose remembered ack is <c>Failed</c> is
/// re-attempted only while that deployment is still this node's latest answer — retrying an OLDER failed
/// deployment after a newer one was applied would roll the node back.
/// </summary>
@@ -91,6 +91,14 @@ public class MeshTransportDtoMapperTests
spent.HasDeadlineRemainingMs.ShouldBeTrue();
MeshTransportDtoMapper.FromProto(spent).DeadlineRemaining.ShouldBe(TimeSpan.Zero);
// Out-of-range wire values (only a buggy or hostile sender) are clamped, never thrown on.
var negative = Wire(MeshTransportDtoMapper.ToProto(resend));
negative.DeadlineRemainingMs = long.MinValue;
MeshTransportDtoMapper.FromProto(negative).DeadlineRemaining.ShouldBe(TimeSpan.Zero);
var huge = Wire(MeshTransportDtoMapper.ToProto(resend));
huge.DeadlineRemainingMs = long.MaxValue;
MeshTransportDtoMapper.FromProto(huge).DeadlineRemaining.ShouldBe(TimeSpan.FromDays(1));
var original = Wire(MeshTransportDtoMapper.ToProto(resend with { TargetNodes = null, DeadlineRemaining = null }));
original.HasDeadlineRemainingMs.ShouldBeFalse();
MeshTransportDtoMapper.FromProto(original).DeadlineRemaining.ShouldBeNull();
@@ -217,6 +217,57 @@ public sealed class DriverHostActorRedispatchTests : RuntimeActorTestBase
fetcher.Calls.ShouldBe(3);
}
[Fact]
public void A_resend_of_a_failed_deployment_is_not_retried_while_a_NEWER_applys_ack_is_still_held()
{
// #1111 review: the newer deployment's Applied ack is held on its address-space outcome, so the failed one is
// still this node's latest ANSWER — only the held-ack guard stops the stale re-send from re-applying the older
// revision over the newer one (and the newer one's held Applied ack then going out for a rolled-back node).
var db = NewInMemoryDbFactory();
var fetcher = new ScriptedFetcher(null, EmptyArtifact);
var coordinator = CreateTestProbe();
var publish = CreateTestProbe();
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref,
localRoles: new HashSet<string> { "driver" },
deploymentArtifactCache: new NullArtifactCache(),
fetchAndCacheMode: true,
artifactFetcher: fetcher,
opcUaPublishActor: publish.Ref,
applyOutcomeTimeout: TimeSpan.FromSeconds(60)));
var older = new DispatchDeployment(DeploymentId.NewId(), RevA, CorrelationId.NewId());
actor.Tell(older);
var failed = coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
failed.Outcome.ShouldBe(ApplyAckOutcome.Failed);
// The newer deployment applies; its Applied ack is HELD (the address-space outcome has not landed).
var newer = new DispatchDeployment(DeploymentId.NewId(), RevisionHash.Parse(new string('b', 64)), CorrelationId.NewId());
actor.Tell(newer);
var rebuild = publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
rebuild.DeploymentId.ShouldBe(newer.DeploymentId);
fetcher.Calls.ShouldBe(2);
// A stale re-send of the older, failed deployment: replays Failed, fetches nothing, starts no rebuild.
actor.Tell(older with { TargetNodes = [TestNode.Value] });
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.ShouldBe(failed);
coordinator.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
fetcher.Calls.ShouldBe(2);
// The held ack is still the newer deployment's, and goes out clean when its outcome lands.
actor.Tell(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId, false, null), publish.Ref);
var ack = coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
ack.DeploymentId.ShouldBe(newer.DeploymentId);
ack.Outcome.ShouldBe(ApplyAckOutcome.Applied);
ack.Degraded.ShouldBeFalse();
// Positive control: an ORIGINAL dispatch of the older deployment does fetch — the silence above was the guard.
actor.Tell(older);
publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(r => r.DeploymentId == older.DeploymentId, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
fetcher.Calls.ShouldBe(3);
}
[Fact]
public void A_late_resend_bounds_the_address_space_wait_to_the_deadline_it_carries()
{