diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs
index 6d16046e0..e673787f3 100644
--- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs
+++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs
@@ -98,6 +98,9 @@ public sealed class OriginHistoryPort(OtOpcUaNodeManager nodeManager) : IOriginH
///
public static int RemoteSlots(int batches) => Math.Max(1, batches - Math.Max(1, batches / 4));
+ /// Remote slots currently free (diagnostics/tests).
+ public int AvailableRemoteSlots => Slots().CurrentCount;
+
private SemaphoreSlim Slots()
{
if (_remoteSlots is { } slots) return slots;
@@ -139,6 +142,7 @@ public sealed class OriginHistoryPort(OtOpcUaNodeManager nodeManager) : IOriginH
// ONE scope per request: it snapshots the serving incarnation at first use (H1 review note).
var scope = new WiringObservedScope(new PeerHistoryContinuationScope(cursors.Store, principal, servingIncarnation));
// Remote peers never take the slots reserved for local clients (H2 review): full => BadTooManyOperations, unsent.
+ cancellationToken.ThrowIfCancellationRequested();
var slots = Slots();
if (!slots.Wait(0))
return Task.FromResult>(items
@@ -155,7 +159,8 @@ public sealed class OriginHistoryPort(OtOpcUaNodeManager nodeManager) : IOriginH
return new OriginHistoryOutcome(status, o.Values.Select(Sample).ToArray(), o.ContinuationPoint,
scope.Faulted && status == StatusCodes.BadInternalError);
}).ToArray();
- }, cancellationToken);
+ }, CancellationToken.None); // NOT the request token: a cancelled Task.Run never runs the delegate, and the slot would leak.
+ // (The token still reaches ReadHistory, which ends in-flight nodes BadTimeout.)
}
private static OriginHistorySample Sample(Opc.Ua.DataValue value)
diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/OriginHistoryLimiterTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/OriginHistoryLimiterTests.cs
index ac39a5aa1..52a248965 100644
--- a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/OriginHistoryLimiterTests.cs
+++ b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/OriginHistoryLimiterTests.cs
@@ -52,6 +52,36 @@ public sealed class OriginHistoryLimiterTests(HistoryReadPortServerFixture fx) :
}
}
+ [Fact]
+ public async Task A_pre_cancelled_read_returns_its_remote_slot()
+ {
+ await fx.Gate.WaitAsync(TestContext.Current.CancellationToken);
+ var source = new BlockingSource();
+ source.Release.TrySetResult(); // reads complete at once
+ try
+ {
+ fx.Nm.HistorianDataSource = source;
+ var port = new OriginHistoryPort(fx.Nm);
+ var details = new DataHistoryRawDetails(new DateTimeOffset(T0), new DateTimeOffset(T0.AddSeconds(5)), 1, false);
+ var item = new OriginHistoryItem(AddressSpaceRealm.Uns, "port/dbl", "peer:uns", null, null);
+ var slots = OriginHistoryPort.RemoteSlots(fx.Nm.MaxConcurrentHistoryReadBatches);
+ using var cancelled = new CancellationTokenSource();
+ await cancelled.CancelAsync();
+ for (var i = 0; i < slots + 3; i++)
+ await Should.ThrowAsync(() => port.ReadAsync(new OriginHistoryCursors("peer", "c" + i, 4), "peer",
+ () => "inc", details, [item], cancelled.Token));
+ port.AvailableRemoteSlots.ShouldBe(slots, "every cancelled dispatch gave its slot back");
+ var served = (await port.ReadAsync(new OriginHistoryCursors("peer", "ok", 4), "peer", () => "inc", details, [item],
+ CancellationToken.None)).Single();
+ served.StatusCode.ShouldNotBe(StatusCodes.BadTooManyOperations.Code);
+ }
+ finally
+ {
+ fx.Nm.HistorianDataSource = NullHistorianDataSource.Instance;
+ fx.Gate.Release();
+ }
+ }
+
private sealed class BlockingSource : IHistorianDataSource
{
private int _calls;