diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverInstanceActor.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverInstanceActor.cs index e0d9cafc6..98c5046aa 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverInstanceActor.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverInstanceActor.cs @@ -1519,10 +1519,7 @@ public sealed class DriverInstanceActor : ReceiveActor, IWithTimers } catch { - var outcome = msg.Claim.Complete(RichRejected) ? RichRejected : RichUnknown; - if (outcome == RichRejected) physical.TrySetResult(outcome); - else msg.Claim.Complete(outcome); - return outcome; + return FailRichAttempt(msg.Claim, physical); } }); try @@ -1534,9 +1531,7 @@ public sealed class DriverInstanceActor : ReceiveActor, IWithTimers } catch { - outcome = msg.Claim.Complete(RichRejected) ? RichRejected : RichUnknown; - if (outcome == RichRejected) physical.TrySetResult(outcome); - else msg.Claim.Complete(outcome); + outcome = FailRichAttempt(msg.Claim, physical); } // #964: when the physical write has finished, its late-write slot must be back BEFORE the caller // hears the outcome or the actor frees the tag — both let a next write to the same target reach @@ -1551,6 +1546,29 @@ public sealed class DriverInstanceActor : ReceiveActor, IWithTimers finally { self.Tell(new WriteCompleted(msg.Ticket.Proof.DriverReference, incarnation)); } } + /// + /// Settles a rich write whose invoker attempt failed or was abandoned. A claim that never dispatched can never + /// dispatch any more once terminal (dispatch is a CAS from pending), so no driver call exists or can start: + /// the physical task is completed with the claim's pre-dispatch outcome, releasing the late-write slot, + /// target and ticket. That holds whether THIS path rejected the claim or someone else (an external + /// pre-dispatch deadline) already had — #967: the latter used to leave the physical task pending forever, + /// reserving the slot and target for the process lifetime. Only a DISPATCHED claim is left to the driver's + /// own completion (it may still be running) and answered Unknown. + /// + private static DataWriteOutcome FailRichAttempt(WriteClaim claim, TaskCompletionSource physical) + { + if (claim.Complete(RichRejected) || !claim.IsDispatched) + { + // CurrentOutcome is null only while another completer is mid-publish; any pre-dispatch completion is a + // RejectedBeforeDispatch, so RichRejected is the same state. + var outcome = claim.CurrentOutcome ?? RichRejected; + physical.TrySetResult(outcome); + return outcome; + } + claim.Complete(RichUnknown); + return RichUnknown; + } + private static async Task ObserveRichDriver(Task> task, TaskCompletionSource physical) { diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataDriverOutcomeTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataDriverOutcomeTests.cs index a719a240c..9e1f779b7 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataDriverOutcomeTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataDriverOutcomeTests.cs @@ -231,6 +231,34 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase driver.Calls.ShouldBe(0); } + [Fact] + public async Task Invoker_failing_before_the_call_site_on_an_externally_rejected_claim_releases_the_slot_and_target() + { + // #967: the invoker (e.g. an open breaker) throws without ever running the dispatch delegate, after the + // claim was already completed RejectedBeforeDispatch by someone else. Nothing reached the driver, but the + // physical task was never completed, so the late-write slot, the target and the ticket stayed reserved + // for the process lifetime and every later write to the tag was refused as capacity exhausted. + var driver = new RichDriver(); + var invoker = new PausedWriteInvoker { FailFirstWithoutCalling = true }; + var actor = Connect(driver, new RecordingGate(), new LateWriteObserver(1, TimeSpan.FromMinutes(1)), invoker); + var (ticket, claim) = NewWrite("breaker-open", 1); + var response = actor.Ask(new DriverInstanceActor.WriteAttributeWithOutcome(ticket, claim), Timeout, TestContext.Current.CancellationToken); + await invoker.Entered.Task.WaitAsync(Timeout, TestContext.Current.CancellationToken); + + claim.Complete(new(DataWriteState.RejectedBeforeDispatch, 0x800A0000u, "deadline before dispatch")).ShouldBeTrue(); + invoker.Release.TrySetResult(); + // Nothing was dispatched, so the caller hears the claim's own pre-dispatch rejection, not "unknown". + (await response.WaitAsync(Timeout, TestContext.Current.CancellationToken)).State.ShouldBe(DataWriteState.RejectedBeforeDispatch); + claim.IsDispatched.ShouldBeFalse(); + driver.Calls.ShouldBe(0); + + var (nextTicket, nextClaim) = NewWrite("after-breaker", 2); + var next = await actor.Ask(new DriverInstanceActor.WriteAttributeWithOutcome(nextTicket, nextClaim), + Timeout, TestContext.Current.CancellationToken); + next.State.ShouldBe(DataWriteState.DriverConfirmedSuccess, next.Diagnostic); + driver.Calls.ShouldBe(1); + } + [Fact] public async Task Queued_write_revalidates_after_an_earlier_write_finishes() { @@ -688,6 +716,10 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase public TaskCompletionSource Entered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); public TaskCompletionSource Release { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); public bool SawIdempotent { get; private set; } + /// When set, the FIRST write call fails after release without ever invoking the call site — the + /// shape of an open circuit breaker / bulkhead rejection. Later calls pass straight through. + public bool FailFirstWithoutCalling { get; init; } + private int _writeCalls; public ValueTask ExecuteAsync(DriverCapability capability, string hostName, Func> callSite, CancellationToken cancellationToken) => callSite(cancellationToken); public ValueTask ExecuteAsync(DriverCapability capability, string hostName, @@ -696,8 +728,11 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase Func> callSite, CancellationToken cancellationToken) { SawIdempotent = isIdempotent; + var first = Interlocked.Increment(ref _writeCalls) == 1; + if (FailFirstWithoutCalling && !first) return await callSite(cancellationToken); Entered.TrySetResult(); await Release.Task; + if (FailFirstWithoutCalling) throw new InvalidOperationException("circuit open"); return await callSite(cancellationToken); } }