From 69014cc4f880d7ceb4e2a6e4a1302424c71b26d0 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 24 Nov 2026 14:48:28 -0500 Subject: [PATCH] =?UTF-8?q?fix(grpc-data):=20A2=20review=20B1=20=E2=80=94?= =?UTF-8?q?=20a=20full=20condition=20queue=20back-pressures=20instead=20of?= =?UTF-8?q?=20resetting=20the=20stream?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit budget = Max(1, …) pulled a chunk on a full queue every 100 ms tick, TryWrite failed and the stream was reset, so a backlog above MaxPendingFrames chunks never completed its snapshot. Max(0, …) produces nothing until the consumer drains. Test: 25-chunk backlog, stalled sink, 20 ticks ⇒ no reset, snapshot completes (red before). --- .../DataSubscriptionSession.Conditions.cs | 4 +++- .../GrpcDataConditionStreamTests.cs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/DataSubscriptionSession.Conditions.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/DataSubscriptionSession.Conditions.cs index af9e9bbd7..b558f09d4 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/DataSubscriptionSession.Conditions.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/DataSubscriptionSession.Conditions.cs @@ -135,7 +135,9 @@ internal sealed partial class DataSubscriptionSession } if (items.Length == 0) return Task.CompletedTask; var queue = ConditionOutbound; - var budget = Math.Max(1, _limits.MaxPendingFrames - queue.Reader.Count); + // Review B1: a full queue is back-pressure — produce nothing (the dequeue re-ticks the pump). Never pull a chunk + // that cannot be queued: the stream would then have to reset on every tick while the backlog drains. + var budget = Math.Max(0, _limits.MaxPendingFrames - queue.Reader.Count); var idle = 0; while (budget > 0 && idle < items.Length && Volatile.Read(ref _closing) == 0 && Volatile.Read(ref _closed) == 0) { diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataConditionStreamTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataConditionStreamTests.cs index 54c34d6da..073542736 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataConditionStreamTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataConditionStreamTests.cs @@ -119,6 +119,23 @@ public sealed class GrpcDataConditionStreamTests reset.Diagnostic.ShouldNotBeNull().ShouldContain("overflow"); } + [Fact] + public async Task A_backlog_above_the_pending_frame_limit_back_pressures_a_stalled_sink_instead_of_resetting() + { + // 3,000 records = 24 record chunks + a begin chunk, above the session's 16 pending frames (review B1). + using var f = new ConditionFixture(conditions: 3000, maxConditions: 5000); + await using var stream = await f.OpenAsync(); + await stream.ReadAckAsync(); + // The sink stalls: nothing is read while the session's 100 ms tick keeps running the pump on a full queue. + for (var i = 0; i < 20; i++) { f.Tick(); await Task.Delay(25, Ct); } + var snapshot = await stream.ReadSnapshotAsync(); + snapshot.Begin.Total.ShouldBe(3000); + snapshot.Records.Count.ShouldBe(3000); + snapshot.Complete.ShouldBeTrue("the snapshot completes once the sink drains"); + stream.Ended.ShouldBeFalse("a full queue is back-pressure, not a stream reset"); + f.StreamOf(stream)!.Snapshots.ShouldBe(1); + } + [Fact] public async Task Delivery_priority_is_control_then_condition_then_value() {