From 30d66fdc7cada1a3d51a0625f2a3bc3cbf569a17 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 24 Nov 2026 07:11:06 -0500 Subject: [PATCH] =?UTF-8?q?fix(grpc-data):=20H3=20review=20round=203=20S5?= =?UTF-8?q?=20=E2=80=94=20a=20cancel=20by=20redeploy/shutdown=20is=20not?= =?UTF-8?q?=20a=20timeout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GrpcDataConnection.ServeAsync mapped RpcException(Cancelled) without a caller cancel to BadTimeout even when the linked channel-retired / scope / shutdown token fired. That case is now BadNoCommunication (resumed: BadContinuationPointInvalid), releases held chunk tokens and stops a split request sending further sub-batches. ProjectedHistoryAccess's outer catch no longer reads any escaped OperationCanceledException as a timeout: only the caller's cancel or the deadline is BadTimeout, otherwise classified as FailCancelled does. Red on revert (client test: scope moved mid-call); the outer-catch change has no dedicated test (nothing reachable throws past ReadGroupAsync's own catch). --- .../GrpcDataConnection.History.cs | 17 +++++++++++++++-- .../DataConnections/ProjectedHistoryAccess.cs | 8 +++++--- .../GrpcDataHistoryClientTests.cs | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/GrpcDataConnection.History.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/GrpcDataConnection.History.cs index 39d37782a..c61bfc20c 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/GrpcDataConnection.History.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/GrpcDataConnection.History.cs @@ -181,7 +181,7 @@ public sealed partial class GrpcDataConnection : IDataHistoryService var call = await ServeAsync(selected, endpoint, request with { Budget = request.Budget with { MaxBytes = left } }, items, chunk, start, cancellationToken).ConfigureAwait(false); served = call.Results; - failed = call.EndpointFailed; // answered, but the endpoint failed mid-way: send it nothing more (nit a) + failed = call.EndpointFailed; // answered, but the endpoint failed or retired mid-way: send it nothing more (nit a) } catch (Exception ex) when (offset > 0 && !cancellationToken.IsCancellationRequested && ex is InvalidOperationException or ArgumentException or RpcException or HttpRequestException) @@ -230,7 +230,7 @@ public sealed partial class GrpcDataConnection : IDataHistoryService /// /// Reads on one selected endpoint and assembles its chunks. Results null = the endpoint failed before answering (a fresh - /// read may move); EndpointFailed = it answered (per node) but failed on the way, so a caller splitting a + /// read may move); EndpointFailed = it answered (per node) but failed or retired on the way, so a caller splitting a /// request sends it nothing more. Anything that escapes first releases every origin token this assembly holds — finished /// nodes' continuations, chunk tokens, and the tokens of a response it rejected (H3 review round 3 S6). /// @@ -282,6 +282,16 @@ public sealed partial class GrpcDataConnection : IDataHistoryService foreach (var k in map) final[k] ??= new(batch[k].CorrelationId, BadHistoryOperationUnsupported, [], null); break; } + catch (Exception ex) when (!cancellationToken.IsCancellationRequested && Retiring(selected) + && ex is OperationCanceledException or RpcException { StatusCode: StatusCode.Cancelled }) + { + // Not the caller and not a deadline: the channel retired, the scope moved or the connection is shutting down + // (a redeploy). Not a timeout and not an endpoint fault (round 3 S5): the read cannot continue here. + if (!first) ReleaseChunkTokens(selected, current); + foreach (var k in map) final[k] ??= new(batch[k].CorrelationId, batch[k].Continuation is not null ? BadContinuationPointInvalid : BadNoCommunication, [], null); + endpointFailed = true; + break; + } catch (RpcException ex) when (ex.StatusCode is StatusCode.DeadlineExceeded or StatusCode.Cancelled && !cancellationToken.IsCancellationRequested) { // The call's own deadline, or the origin cancelling it at ITS unary timeout (the effective ceiling is @@ -367,6 +377,9 @@ public sealed partial class GrpcDataConnection : IDataHistoryService } } + /// The call was cancelled by the connection itself: its channel retired, its scope moved, or it is shutting down. + private bool Retiring(Selected selected) => + selected.Channel.Retired.IsCancellationRequested || _scope.CancellationToken.IsCancellationRequested || _shutdown.IsCancellationRequested; /// Releases continuations on their issuing endpoints (); best effort. public async Task ReleaseHistoryAsync(IReadOnlyList continuations, CancellationToken cancellationToken) diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedHistoryAccess.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedHistoryAccess.cs index a68f634ee..d79634496 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedHistoryAccess.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/ProjectedHistoryAccess.cs @@ -199,14 +199,16 @@ public sealed class ProjectedHistoryAccess : IProjectedHistoryAccess, IDisposabl } catch (Exception ex) when (ex is not OutOfMemoryException) { - // Last resort (H3 review D1): whatever escaped, fail per node — a timeout when the deadline fired — and release every + // Last resort (H3 review D1): whatever escaped, fail per node — a timeout only when the caller or the deadline + // cancelled; any other cancel (a retired lease) is classified as FailCancelled does (round 3 S5) — and release every // origin cursor this request still owns. Nothing escapes to the SDK override. - var timedOut = cancellationToken.IsCancellationRequested || Remaining() <= TimeSpan.Zero || ex is OperationCanceledException; + var timedOut = cancellationToken.IsCancellationRequested || Remaining() <= TimeSpan.Zero; for (var i = 0; i < nodes.Count; i++) { if (outcomes[i] is not null) continue; + var resumed = nodes[i].Resume is RemoteCursor; nodes[i].Resume?.ReleaseEvenIfClaimed(); - outcomes[i] = Fail(timedOut ? BadTimeout : BadNoCommunication); + outcomes[i] = Fail(timedOut ? BadTimeout : ex is OperationCanceledException && resumed ? BadContinuationPointInvalid : BadNoCommunication); } return outcomes.Select(o => o!).ToArray(); diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataHistoryClientTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataHistoryClientTests.cs index de76e9f29..95be22d99 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataHistoryClientTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataHistoryClientTests.cs @@ -351,6 +351,25 @@ public sealed class GrpcDataHistoryClientTests ReleasedCount(a, [7, 7]).ShouldBeGreaterThan(0, "n1's chunk tokens are released"); } + [Fact] + [Trait("Phase", "H3")] + public async Task A_call_cancelled_by_a_scope_move_is_not_a_timeout() + { + // S5: neither the caller nor the deadline cancelled the call — the connection's scope moved (a redeploy). + DataConnectionManager? manager = null; + var a = new Endpoint("a") { HangOnDataCall = 1 }; + await using var connection = Connect([a], onManager: m => manager = m); + var request = new DataHistoryReadRequest(Request().Details, + [new("fresh", View), new("resumed", View, null, GrpcDataConnection.Wrap(0, "boot-a", [1, 2]))], Budget); + var read = connection.ReadHistoryAsync(request, TestContext.Current.CancellationToken); + var deadline = DateTime.UtcNow.AddSeconds(10); + while (Volatile.Read(ref a.DataCalls) < 1 && DateTime.UtcNow < deadline) await Task.Delay(10, TestContext.Current.CancellationToken); + manager!.ApplyScope(new Dictionary>()); + var results = await read; + results[0].StatusCode.ShouldBe(0x80310000u, "fresh: BadNoCommunication, not BadTimeout"); + results[1].StatusCode.ShouldBe(0x804A0000u, "resumed: BadContinuationPointInvalid"); + } + [Fact] public async Task A_failure_mid_assembly_releases_the_origin_chunk_tokens_it_holds() {